I’ve been googling & trying to get this myself but can’t quite get it…
QUESTION: What regular expression could be used to select text BETWEEN (but not including) the delimiter text. So as an example:
Start Marker=ABC
Stop Marker=XYZ
---input---
This is the first line
And ABCfirst matched hereXYZ
and then
again ABCsecond matchXYZ
asdf
------------
---expected matches-----
[1] first matched here
[2] second match
------------------------
Thanks
Standard or extended regex syntax can’t do that, but what it can do is create match groups which you can then select. For instance:
will store anything between
ABCandXYZas\1(otherwise known as group 1).If you’re using PCREs (Perl-Compatible Regular Expressions), lookahead and lookbehind assertions are also available — but groups are the more portable and better-performing solution. Also, if you’re using PCREs, you should use
*?to ensure that the match is non-greedy and will terminate at the first opportunity.You can test this yourself in a Python interpreter (the Python regex syntax is PCRE-derived):