I have a problem with a regex, i should delete the NOP line only when the previous line starting with #/ and ends with the letter E
SFIFIPII2#/TEST/APPTEST1/KJOBA01
FOLLOWS KTEST1
SFIFIPII2#/TEST3/APPTEST12/KJOBA03E
NOP
FOLLOWS KTEST14D
SFIFIPII4#/TEST5/APPTEST3/KJOBA04
NOP
FOLLOWS KTEST15
SFIFIPII23#/TEST2/APPTEST13/KJOBA018
FOLLOWS KTEST15
SFIFIPII26#/TEST7/APPTEST18/KJOBA01AE
AT 0600
NOP
FOLLOWS KTEST1B
SFIFIPII23#/TEST2/APPTEST11/KJOBA01C
AT 0600
NOP
FOLLOWS KTEST1S
SFIFIPII2A#/TESTD/APPTEST1F/KJOBA01D
FOLLOWS KTEST1S
SFIFIPII2N#/TEST/APPTEST1V/KJOBTESTE
AT 0600
NOP
FOLLOWS KTEST11
FOLLOWS KTEST12
FOLLOWS KTEST11
SFIFIPII2#/TEST/APPTEST1/KJOBA01LS
NEEDS 1 MANAGER_XA#RA0E2AB
FOLLOWS KTEST12
SFIFIPII2#/TEST3/APPTEST12/KJOBA08E
NOP
FOLLOWS KTEST14D
with this regex i was able to locate the line starting with #/ and ending with letter E
^.*#\/.*E$
in this example the result is:
SFIFIPII2#/TEST3/APPTEST12/KJOBA03E
SFIFIPII26#/TEST7/APPTEST18/KJOBA01AE
SFIFIPII2N#/TEST/APPTEST1V/KJOBTESTE
SFIFIPII2#/TEST3/APPTEST12/KJOBA08E
however i don't know how to delete its corresponding NOP line:
there are two cases, one with the NOP line immediately after it, the second case is that NOP line it is found two lines after.
SFIFIPII2#/TEST3/APPTEST12/KJOBA03E
NOP
FOLLOWS KTEST14D
SFIFIPII26#/TEST7/APPTEST18/KJOBA01AE
AT 0600
NOP
FOLLOWS KTEST1B
https://regex101.com/r/FhdPKf/1
i'm use a editor text with search and replace regex support. (textpad, editpad, pspad)
some advice, thanks.
Regards
Italo
CodePudding user response:
You could use a capture group to keep what you want after the replacement, and match the line with NOP to be removed.
In the replacement use capture group 1.
^(.*#\/.*E(?:\r?\n(?![^\S\r\n]*NOP$).*)*)\r?\n[^\S\r\n]*NOP$
^
Start of string(
Capture group 1.*#\/.*E
Match a line that contains#/
and ends onE
(?:
Non capture group\r?\n(?![^\S\r\n]*NOP$).*
Match a newline, and the rest of the line if it does not start with optional spaces and NOP
)*
Close group and optionally repeat to match all lines
)
Close group 1\r?\n[^\S\r\n]*NOP
Match a newline, optional whitespace chars without newlines andNOP
$
End of string
See a regex demo
You might for example also make the pattern a bit more restricted to not cross matching empty lines, or lines that also contain /#
and end with an E
char inbetween before matching NOP
:
^(.*#/.*E(?:\r?\n(?![^\S\r\n]*(?:NOP)?$|.*#/.*E$).*)*)\r?\n[^\S\r\n]*NOP$
See another regex demo