2016-07-26 41 views
0

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

String input = "!!!Error deploying file order\\POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1!!!Error deploying file order\\POST_ORDER_"; 
Pattern p = Pattern.compile("(?<=\\b!!!Error deploying file\\b).*?(?=\\b!!!Error deploying file\\b)"); 
Matcher m = p.matcher(input); 
List<String> matches = new ArrayList<String>(); 
while (m.find()) { 
    System.out.println("A " + m.group()); 
} 
+0

什么是你想在这里实现? – NAIT

+0

以获取“!!!错误部署文件”文本字段之间的文本。 –

+0

把'。*?'放在'()' –

回答

1

更新答案

使用正则表达式

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

上面的图案的说明。

  1. *? - 零点和无限时间之间匹配单个字符
  2. \ S - 匹配任何空白字符
  3. \ S - 匹配任何非空白字符

实施例的代码:

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. 找到的值:order \ POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22 -JUL-16 08:07:Chathura aBhanakana1
  2. 实测值: 顺序\ POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA

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

+0

这是工作。但事情是,如果该行有超过两个“!!!错误部署文件”这个words.it采取第一个和最后一个.Ex:!!!错误部署文件Chathura aBhanakana1 !!!错误部署fileAAA !!!错误部署文件,,:,在这里,我不能把这两个字内的模式 –

+0

@chathuraBhanaka对不起,我不明白你在说什么,请提供一个例子,并说出你想要的输出。 例如 - **我的字符串是''“'',输出应该是''”'。** 如果您有多个字符串,请提供所有不同的字符串,并输出您期望的输出。 – Ravikumar

+0

!!!错误部署文件顺序\ POST_ORDER_UpdateTaxAmountCurrInCo.sql 08年7月22日08:07:Chathura aBhanakana1 !!!错误部署文件顺序\ POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA !!!错误部署文件顺序\ POST; ........如果你把它当作你的代码中的一行,我想要的是:: order \ POST_ORDER_UpdateTaxAmountCurrInCo.sql在22-JUL-16 08:07:Chathura aBhanakana1和order \ POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAAAAAAAAAAAAAAAAAAA this.because这些之间的模式 –

相关问题