2016-04-23 54 views
0

我想分割使用2空行的段落。我嘗試使用String.split(),StringTokenizerStringUtils類,但沒有工作。如何使用空行分割段落?

這裏是我的代碼:

DeviceNames (interfacename) # show commands 
    Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
    O - OSPF, IA - OSPF inter area 
    N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
    E1 - OSPF external type 1, E2 - OSPF external type 2 
    i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
    * - candidate default 

    S*  0.0.0.0/0 [1/0] via 172.16.0.1, internal 
    S  10.100.8.0/21 [10/0] is directly connected, vlink10 
    C  0.0.2.0/24 is directly connected, internal 
    C  0.0.2.0/24 is directly connected, wan1 


    DeviceNames (interfacename) # show commands 
    Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
    O - OSPF, IA - OSPF inter area 
    N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
    E1 - OSPF external type 1, E2 - OSPF external type 2 
    i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
    * - candidate default 

    S*  0.0.0.0/0 [10/0] is directly connected, vlink21 
       [10/0] is directly connected, vlink30 
    C  0.0.2.0/24 is directly connected, vlink30 
    C  0.0.2.0/32 is directly connected, vlink30 
    C  0.0.2.0/24 is directly connected, vlink21 
    C  0.0.2.0/32 is directly connected, vlink21 


    DeviceNames (interfacename) # show commands 
    Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
    O - OSPF, IA - OSPF inter area 
    N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
    E1 - OSPF external type 1, E2 - OSPF external type 2 
    i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
    * - candidate default 

    S*  0.0.0.0/0 [10/0] is directly connected, vlink11 
       [10/0] is directly connected, vlink31 
    C  0.0.1.0/24 is directly connected, vlink31 
    C  0.0.1.0.1.1/32 is directly connected, vlink31 

我想分裂上面的代碼,如:

DeviceNames (interfacename) # show commands 
    Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
    O - OSPF, IA - OSPF inter area 
    N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
    E1 - OSPF external type 1, E2 - OSPF external type 2 
    i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
    * - candidate default 

    S*  0.0.0.0/0 [1/0] via 172.16.0.1, internal 
    S  10.100.8.0/21 [10/0] is directly connected, vlink10 
    C  0.0.2.0/24 is directly connected, internal 
    C  0.0.2.0/24 is directly connected, wan1 

下一個:

DeviceNames (interfacename) # show commands 
    Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
    O - OSPF, IA - OSPF inter area 
    N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
    E1 - OSPF external type 1, E2 - OSPF external type 2 
    i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
    * - candidate default 

    S*  0.0.0.0/0 [10/0] is directly connected, vlink21 
       [10/0] is directly connected, vlink30 
    C  0.0.2.0/24 is directly connected, vlink30 
    C  0.0.2.0/32 is directly connected, vlink30 
    C  0.0.2.0/24 is directly connected, vlink21 
    C  0.0.2.0/32 is directly connected, vlink21 

下一個:

DeviceNames (interfacename) # show commands 
    Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
    O - OSPF, IA - OSPF inter area 
    N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
    E1 - OSPF external type 1, E2 - OSPF external type 2 
    i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
    * - candidate default 

    S*  0.0.0.0/0 [10/0] is directly connected, vlink11 
       [10/0] is directly connected, vlink31 
    C  0.0.1.0/24 is directly connected, vlink31 
    C  0.0.1.0.1.1/32 is directly connected, vlink31 

回答

0

解決方案可以逐行讀取文件並將每行存儲到變量(或數組,或您需要的)中。 當你讀你可以檢查該行是在這樣一個空行:

String newline = System.getProperty("line.separator"); // this is for getting proper new line; 

boolean isNewline = myLine.startsWith(newline); //if your line start with newLine symbol 

如果isNewLine是真實的,那麼你可以設置一個標誌,如果重新換行是空的,那麼你剛纔找到你的段落。

0

String#split()用正則表達式應該可以工作。

例如:

String[] paragraphs = input.split("\\n\\n\\n");

注:該作品使用Unix只換行。

0

這可以用簡單的regex來完成:

嘗試:

String[] paragraphs = text.split("(?im)(\r?\n){3,}"); 

當變量text包含您的數據。根據樣本輸入數據,上面的語句將把它分成3部分。此解決方案將與在Windows或Linux上創建的文件協同工作,從而與平臺無關。

0

您可以用兩個空行中使用正則表達式之間的分裂代碼:

public static void main(String[] args) 
    { 
    String str=" DeviceNames (interfacename) # show commands\n"+ 
" Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP\n"+ 
" O - OSPF, IA - OSPF inter area\n"+ 
" N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2\n"+ 
" E1 - OSPF external type 1, E2 - OSPF external type 2\n"+ 
" i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area\n"+ 
" * - candidate default\n"+ 
"\n"+ 
" S* 0.0.0.0/0 [1/0] via 172.16.0.1, internal\n"+ 
" S 10.100.8.0/21 [10/0] is directly connected, vlink10\n"+ 
" C 0.0.2.0/24 is directly connected, internal\n"+ 
" C 0.0.2.0/24 is directly connected, wan1\n"+ 
"\n"+ 
"\n"+ 
" DeviceNames (interfacename) # show commands\n"+ 
" Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP\n"+ 
" O - OSPF, IA - OSPF inter area\n"+ 
" N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2\n"+ 
" E1 - OSPF external type 1, E2 - OSPF external type 2\n"+ 
" i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area\n"+ 
" * - candidate default\n"+ 
"\n"+ 
" S* 0.0.0.0/0 [10/0] is directly connected, vlink21\n"+ 
" [10/0] is directly connected, vlink30\n"+ 
" C 0.0.2.0/24 is directly connected, vlink30\n"+ 
" C 0.0.2.0/32 is directly connected, vlink30\n"+ 
" C 0.0.2.0/24 is directly connected, vlink21\n"+ 
" C 0.0.2.0/32 is directly connected, vlink21\n"+ 
"\n"+ 
"\n"+ 
" DeviceNames (interfacename) # show commands\n"+ 
" Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP\n"+ 
" O - OSPF, IA - OSPF inter area\n"+ 
" N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2\n"+ 
" E1 - OSPF external type 1, E2 - OSPF external type 2\n"+ 
" i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area\n"+ 
" * - candidate default\n"+ 
"\n"+ 
" S* 0.0.0.0/0 [10/0] is directly connected, vlink11\n"+ 
" [10/0] is directly connected, vlink31\n"+ 
" C 0.0.1.0/24 is directly connected, vlink31\n"+ 
" C 0.0.1.0.1.1/32 is directly connected, vlink31\n"+ 
""; 
    String[] array = str.split("(?<=\\S)\\n\\n\\n"); 

    for (int i = 0; i < array.length; i++) { 
     System.out.println(String.format("================ %1$d ================\n%2$s", i + 1, array[i])); 
     } 
    } 

輸出:

================ 1 ================ 
DeviceNames (interfacename) # show commands 
Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
O - OSPF, IA - OSPF inter area 
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
E1 - OSPF external type 1, E2 - OSPF external type 2 
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
* - candidate default 

S* 0.0.0.0/0 [1/0] via 172.16.0.1, internal 
S 10.100.8.0/21 [10/0] is directly connected, vlink10 
C 0.0.2.0/24 is directly connected, internal 
C 0.0.2.0/24 is directly connected, wan1 
================ 2 ================ 
DeviceNames (interfacename) # show commands 
Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
O - OSPF, IA - OSPF inter area 
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
E1 - OSPF external type 1, E2 - OSPF external type 2 
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
* - candidate default 

S* 0.0.0.0/0 [10/0] is directly connected, vlink21 
[10/0] is directly connected, vlink30 
C 0.0.2.0/24 is directly connected, vlink30 
C 0.0.2.0/32 is directly connected, vlink30 
C 0.0.2.0/24 is directly connected, vlink21 
C 0.0.2.0/32 is directly connected, vlink21 
================ 3 ================ 
DeviceNames (interfacename) # show commands 
Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP 
O - OSPF, IA - OSPF inter area 
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
E1 - OSPF external type 1, E2 - OSPF external type 2 
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area 
* - candidate default 

S* 0.0.0.0/0 [10/0] is directly connected, vlink11 
[10/0] is directly connected, vlink31 
C 0.0.1.0/24 is directly connected, vlink31 
C 0.0.1.0.1.1/32 is directly connected, vlink31