如何使用java在文本文件的两行之间获取文本

时间:2016-07-26 08:34:38

标签: java regex

如何使用Java在文本文件的两行之间获取文本? 我尝试这样做,但它不起作用:

Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference

1 个答案:

答案 0 :(得分:1)

更新回答

使用正则表达式

String pattern = "([\\s\\S]*?)(!!!Error deploying file)";

上述模式的解释。

  1. *? - 匹配零和无限次之间的单个字符
  2. \ s - 匹配任何空格字符
  3. \ S - 匹配任何非空白字符
  4. 示例代码:

    String line = "!!!Error deploying file order\\POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1!!!Error deploying file order\\POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA !!!Error deploying file order\\POST";
    
    String pattern = "([\\s\\S]*?)(!!!Error deploying file)";
    
    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);
    
    // Now create matcher object.
    Matcher m = r.matcher(line);
    while (m.find( )) {
      String str = m.group(1);
      if(str != null && !str.isEmpty()){
         System.out.println("Found value: " + str );
       }
    } 
    

    输出

    1. 找到值:订单\ POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1
    2. 发现价值: 订单\ POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA
    3. Check output here

      使用拆分方法

      示例代码:

      String line = "!!!Error deploying file order\\POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1!!!Error deploying file order\\POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA !!!Error deploying file order\\POST";
      
      for (String retval: line.split("!!!Error deploying file")){
             System.out.println(retval);
       }
      

      输出:

      1 ) order\POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1
      2) order\POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA
      3) order\POST
      

      Check output here