Problem trying to collapse a word in Java regex

I am trying to parse the 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 goal is to find the MOVE statement matching the given name.
Example: with dest5 I want to find "MOVE src5 TO dest5."

My Java code is:

     String paragraphePart = "PNAME. P000 084500 MOVE src1 TO dest1 P110 084510 MOVE src2 TO dest2 P115 084520 MOVE src3 TO dest3 P115 084530 MOVE src4 TO dest4 P120 084540 MOVE src5 TO dest5.  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 just want to get this line: "MOVE src5 TO dest5". In my regex, I should use something like MOVE. * TO, because I can have this case:

  084540 MOVE P120
     084550 src5 TO dest5.

Here I need to get MOVE P120 084550 src5 TO dest5 and not just src5 TO dest5.

So, how can I tell my regular element to find a MOVE followed by something but not another β€œMOVE” and then β€œTO”?

thanks

[SOLVED]
I use:

  Matcher m = Pattern.compile ("(MOVE (?!. *? MOVE). *? \\ s + TO \\ s + [^ \\.] *" + FieldName + "(\\ s + | \\. | $ )) ", Pattern.DOTALL) .matcher (paragraphePart);

Thanks anubhava !
stack overflow

[NEW PB] Usage
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 to use "dest4" to get this line "MOVE src4 TO dest4", it no longer works. Do you have an idea?

 Matcher m = Pattern.compile("(MOVE(?!.*?MOVE.*?"+fieldName+").*?\\s+\\w+\\s+TO\\s+[^\\.]*"+fieldName+"(\\s+|\\.|$))", Pattern.DOTALL).matcher(paragraphePart); 


+4
source share
2 answers

You can use the following regexp with a negative expression:

 String needle = "dest5"; Matcher m = Pattern.compile("(MOVE(?!.*?MOVE.*?" + needle + ").*?\\s+.+?\\s+TO\\s+" + needle + ")", Pattern.DOTALL).matcher(paragraphePart); 
+1
source

There is no easy way to nullify the whole word, you can only cancel the letter by letter.

It seems to me that the easiest way to do this is to use regular Java code, not a regular expression.

0
source

Source: https://habr.com/ru/post/1390221/


All Articles