简体   繁体   English

如何用空行分割段落?

[英]How do I split the paragraph using empty lines?

I am trying to split paragraph using 2 empty lines. 我正在尝试使用2个空行拆分段落。 I tried using String.split() , StringTokenizer and StringUtils classes, but none worked. 我尝试使用String.split()StringTokenizerStringUtils类,但没有一个起作用。

Here is my code: 这是我的代码:

   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

I want split the above code like : 我想将上面的代码拆分为:

   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

next one: 下一个:

   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

next one : 下一个 :

   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

A solution could be read the file line by line and store every line into a variable (or array, or what you need). 解决方案可以逐行读取文件,并将每一行存储到变量(或数组,或所需的变量)中。 While you are reading you can check if that line is an empty line in this way: 在阅读时,您可以通过以下方式检查该行是否为空行:

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

if isNewLine is true, then you can set a flag, and if next line is again empty, then you have just found your paragraph. 如果isNewLine为true,则可以设置一个标志,如果下一行再次为空,则您刚刚找到了段落。

String#split() with a regular expression should work. 具有正则表达式的String#split()应该可以工作。

For example: 例如:

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

Note: this works with Unix newlines only. 注意:这仅适用于Unix换行符。

This can be done with simple regex: 这可以通过简单的正则表达式来完成:

Try: 尝试:

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

Where variable text contains your data. 其中可变text包含您的数据。 Based on sample input data, statement above will split it into 3 parts. 根据样本输入数据,以上陈述将其分为三部分。 This solution will work with files created on Windows or Linux so platform agnostic. 此解决方案将与在Windows或Linux上创建的文件一起使用,因此与平台无关。

You could split your code by the two empty lines in between using regex: 您可以使用regex将代码分成两个空行:

  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]));
       }  
    }

Output: 输出:

================ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM