I have problem writing a conditional regex. I need to find multiline text with an optional end. Usually it is the "normal end", sometimes I have this additional suffix.
Could some one please support me with this problem.
Here is my example:
Start of Text
some thing in between
some thing in between
some thing in between
Normal end of text
Optional suffix
^(?:Start)[\d\w\s]*?(?(?=Optional.*)Optional.*|Normal.*)
It only matches until "Normal end of text" Even though, the "Optional" is present. What I understood from contional regex, it should find "Optional" and though match "Optional"
Thank you so much.
CodePudding user response:
You can use
^(Start)((?s:.*?))(Normal.*)(\s*Optional.*)?
See the regex demo. Details:
^
- start of string(Start)
- Group 1:Start
((?s:.*?))
- Group 2: any zero or more chars as few as possible(Normal.*)
- Group 3:Normal
and then any zero or more chars other than line break chars as few as possible(\s*Optional.*)?
- an optional Group 4 matching zero or more whitespaces,Optional
and then any zero or more chars other than line break chars as few as possible.