0

How can I get the text between two lines of a text file using Java? I try to do like this but it's not working:

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());
}
f_puras
  • 2,521
  • 4
  • 33
  • 38
chabha
  • 29
  • 8

1 Answers1

1

Updated answer

Using regex

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

Explanation of above pattern.

  1. *? - match a single character between zero and unlimited times
  2. \s - match any white space character
  3. \S - match any non-whitespace character

Example code :

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

Output

  1. Found value: order\POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1
  2. Found value: order\POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAA

Check output here

Using split method

Example code :

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

Output :

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

Ravikumar
  • 891
  • 12
  • 22
  • It's work.But the thing is,if the line has more than two "!!!Error deploying file" this words.it takes the first and last ones.Ex :!!!Error deploying file Chathura aBhanakana1!!!Error deploying fileAAA!!!Error deploying file ,,:,In here i can't take both words inside the pattern – chabha Jul 26 '16 at 09:22
  • @chathuraBhanaka Sorry I didn't understand what you are telling, please provide a example and say what output you want. For example - **My string is this `""`, the output should be this `""`.** If you have more than one string then provide all different strings with what output you are expecting. – Ravikumar Jul 26 '16 at 09:53
  • !!!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;........If you take this as a line in your code.what i want is ::order\POST_ORDER_UpdateTaxAmountCurrInCo.sql at 22-JUL-16 08:07:Chathura aBhanakana1 and order\POST_ORDER_UpdateTaxAmountChathura aBhanakana1AAAAAAAAAAAAAAAAAAAAAA this.because these are in between Pattern – chabha Jul 26 '16 at 10:00
  • @chathuraBhanaka I have updated the answer, you can split the string with **!!!Error deploying file** which will give back a String array with data before and after the regex. – Ravikumar Jul 26 '16 at 11:01
  • use [non-greedy match](http://stackoverflow.com/q/11898998/995714) `"!!!Error deploying file(.*?)!!!Error deploying file"` (notice the ?) to get the first `!!!Error deploying file` end. – phuclv Jul 26 '16 at 11:05
  • @LưuVĩnhPhúc I tried that, its not working. So he wants like this Example: String "My cat best cat excellent cat". Output should be : best, excellent. – Ravikumar Jul 26 '16 at 11:29
  • @ Ravikumar,Thank you.It's Work properly – chabha Jul 26 '16 at 11:34
  • @chathuraBhanaka If my answer solved your problem then please select it as answer. – Ravikumar Jul 26 '16 at 11:38
  • @chathuraBhanaka I have updated my answer, have a look at it. – Ravikumar Jul 26 '16 at 13:26