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…).
I’ve tried the following regex:
(?:\.if|\.else)(.*?)(?:\.else|\.endif)
but it misses code2
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)
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.