【问题标题】:How do I split the paragraph using empty lines?如何使用空行拆分段落?
【发布时间】:2016-04-23 19:41:12
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: java regex parsing


    【解决方案1】:

    解决方案可以逐行读取文件并将每一行存储到变量(或数组,或您需要的)中。 在阅读时,您可以通过以下方式检查该行是否为空行:

    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 为真,那么你可以设置一个标志,如果下一行又是空的,那么你刚刚找到了你的段落。

    【讨论】:

      【解决方案2】:

      String#split() 与正则表达式应该可以工作。

      例如:

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

      注意:这仅适用于 Unix 换行符。

      【讨论】:

        【解决方案3】:

        您可以使用正则表达式将代码拆分为两个空行:

          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
        

        【讨论】:

          【解决方案4】:

          这可以通过简单的正则表达式来完成:

          试试:

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

          变量text 包含您的数据。根据样本输入数据,上述语句将其分为 3 部分。此解决方案适用于在 Windows 或 Linux 上创建的文件,因此与平台无关。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-03-11
            • 1970-01-01
            • 2011-01-10
            • 1970-01-01
            • 1970-01-01
            • 2012-03-17
            • 1970-01-01
            • 2020-09-21
            相关资源
            最近更新 更多