Search code examples
regexregex-group

select block text if match specific line, regex


following the advice from From Review i'm open a new Question thread, the previous Questios it's: delete line after match when end specific letter. regex (delete line after match when end specific letter. regex)

I have another need with another question, should i select to copy in another files an entire block only if NOP line has the previous line starting with #/ and ends with the letter E the block is always start with line SCHEDULE and always finish with line END

in this example is true (NOP line has the previous line starting with #/ and ends with the letter E) and should select all block:

  • select an entire block to cut in another file.
  • the block is always start with line SCHEDULE and always finish with line END
  • only if NOP line has the previous line starting with #/ and ends with the letter E

the block is:

SCHEDULE MANAGER_XA#KAAABBBR 
DESCRIPTION "Added by default."
ON RUNCYCLE KAAABBBR VALIDTO 09/24/2021 $RCG KAAABBBR
:
S89AAAABBB1#/XAAA/XCCCDDDD/KA0EG014
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTE
 NOP
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTS
 NOP
 FOLLOWS MANAGER_XA#KZZZDDDD.KGGGHHHHHE 

END

however, there may be other SCHEDULE block with jobs that end with the letter E but are not NOP and should not be taken into consideration.

SCHEDULE MANAGER_XA#KBBBCCCR 
DESCRIPTION "Added by default."
ON RUNCYCLE KAAABBBR VALIDTO 09/24/2021 $RCG KBBBCCCR 
:
S89AAAABBB1#/XBBB/XCCCDDDD/KA0EG014
 FOLLOWS KABBBCCC3

S89AAAABBB1#/XBBB/XCCCDDDD/KAHHHTTTE
 FOLLOWS KABBBCCC3

S89AAAABBB1#/XBBB/XCCCDDDD/KAHHHTTTTTS
 NOP
 FOLLOWS MANAGER_XA#KZZZDDDD.KGGGHHHHHE 

END

with this code search line starting with #/ and ends with the letter E and remove line NOP (thanks The fourth bird)

^(.*#\/.*E(?:\r?\n(?![^\S\r\n]*NOP$).*)*)\r?\n[^\S\r\n]*NOP$

any advice please.

Thansk

Regards.

Italo

EDIT UPDATE:

yes, the second example it's ok but

when it finds the match, it must select the entire block it belongs to copy text block to another file.

when match it's true

S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTE
 NOP

it should select all text block (start line SCHEDULE a finish in line END), this:

SCHEDULE MANAGER_XA#KAAABBBR 
DESCRIPTION "Added by default."
ON RUNCYCLE KAAABBBR VALIDTO 09/24/2021 $RCG KAAABBBR
:
S89AAAABBB1#/XAAA/XCCCDDDD/KA0EG014
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTE
 NOP
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTS
 NOP
 FOLLOWS MANAGER_XA#KZZZDDDD.KGGGHHHHHE 

END

Regards.

Italo


Solution

  • (?s)^SCHEDULE(?:.(?!^END$))*?#\/[^\r\n]*E\r?\n[ \t]*NOP$.*?^END$
    

    If in your regex tool doesn´t work (?s) (dot matches also newlines), then replace . with (?:.|\s)or any expression for any character.

    Please note that the quantifiers *? are enforced lazy to avoid taking characters from the next block (after END).

    Regex101 test