2016-04-23 143 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