I'm trying to parse following Cobol code in Java.
PNAME. P000 084500 MOVE src1 TO dest1 P110 084510 MOVE src2 TO dest2 P111 084520 MOVE src3 TO dest3 P115 084530 MOVE src4 TO dest4 P120 084540 MOVE src5 TO dest5. P140 084550 PERFORM TOTO THRU TOTO-FN. P310
My target is to found the MOVE statement corresponding to a given name.
Ex : with dest5 I want to found "MOVE src5 TO dest5."
My Java code is :
String paragraphePart = "PNAME. P000 084500 MOVE src1 TO dest1 P110 084510 MOVE src2 TO dest2 P111 084520 MOVE src3 TO dest3 P115 084530 MOVE src4 TO dest4 P120 084540 MOVE src5 TO dest5. P140 084550 PERFORM TOTO THRU TOTO-FN. P310"; Matcher m = Pattern.compile("MOVE((?!.*MOVE.*).)*TO\\s+[^\\.]*"+"dest5"+"(\\s+|\\.|$)",Pattern.MULTILINE).matcher(paragraphePart); while(m.find()){ //treatement on m.group(0) }
m.group(0) contains :
MOVE src1 TO dest1 P110 084510 MOVE src2 TO dest2 P111 084520 MOVE src3 TO dest3 P115 084530 MOVE src4 TO dest4 P120 084540 MOVE src5 TO dest5.
But I only want to get this line : "MOVE src5 TO dest5."
In my regex I've to use something like MOVE.*TO because I can have this case :
084540 MOVE P120 084550 src5 TO dest5.
Here I have to get MOVE P120 084550 src5 TO dest5 and not just src5 TO dest5.
So how could i tell to my regex find MOVE followed by anything - but not another "MOVE" - and followed by "TO" ?
Thanks
[SOLVED]
I use :
Matcher m = Pattern.compile("(MOVE(?!.*?MOVE).*?\\s+TO\\s+[^\\.]*"+fieldName+"(\\s+|\\.|$))", Pattern.DOTALL).matcher(paragraphePart);
Thank you anubhava!
https://stackoverflow.com/a/8803309/1140748
[NEW PB]
Using
Matcher m = Pattern.compile("(MOVE(?!.*?MOVE).*?\\s+TO\\s+[^\\.]*"+"dest5"+"(\\s+|\\.|$))", Pattern.DOTALL).matcher(paragraphePart);
I can get MOVE src5 TO dest5. But if I try using "dest4" to get this line "MOVE src4 TO dest4" it doesn't work anymore. Have you an idea?
Matcher m = Pattern.compile("(MOVE(?!.*?MOVE.*?"+fieldName+").*?\\s+\\w+\\s+TO\\s+[^\\.]*"+fieldName+"(\\s+|\\.|$))", Pattern.DOTALL).matcher(paragraphePart);