Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular expression to match text between specific keywords

I’m looking for a regular expression that matches the text between the keywords .if .else .elseif and .endif

Example:

.if CONDITION1
code1
.elseif CONDITION2
code2
.else
code3
.endif

Ideally, the regular expression would just match code1, code2 and code3, but it is ok if it matches the text after the keywords as well (CONDITION1, CONDITION2…).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried the following regex:

(?:\.if|\.else)(.*?)(?:\.else|\.endif)

but it misses code2

Demo

The regular expression has to work as well when there is no .elseif and/or .else.

>Solution :

Your issue is that you are effectively trying to capture an overlapping match from the if to the elseif clause. You can workaround that by making the final group in your regex a lookahead instead.

(?:\.if|\.else(?:if)?)(.*?)(?=\.else|\.endif)

Demo on regex101

Note in terms of avoiding capturing the conditions (or simplifying post-processing them out), you would need to change the first group of the regex to allow for an optional if after the .else. I’ve made that change above, but it’s not strictly necessary to make the regex work as yours currently does.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading