The following regex finds text between substrings FTW and ODP.
/FTW(((?!FTW|ODP).)+)ODP+/
What does the (?!
...)
do?
(?!regex)
is a zero-width negative lookahead. It will test the characters at the current cursor position and forward, testing that they do NOT match the supplied regex, and then return the cursor back to where it started.
The whole regexp:
/
FTW # Match Characters 'FTW'
( # Start Match Group 1
( # Start Match Group 2
(?!FTW|ODP) # Ensure next characters are NOT 'FTW' or 'ODP', without matching
. # Match one character
)+ # End Match Group 2, Match One or More times
) # End Match Group 1
OD # Match characters 'OD'
P+ # Match 'P' One or More times
/
So - Hunt for FTW
, then capture while looking for ODP+
to end our string. Also ensure that the data between FTW
and ODP+
doesn't contain FTW
or ODP
From perldoc:
A zero-width negative look-ahead assertion. For example
/foo(?!bar)/
matches any occurrence of "foo
" that isn't followed by "bar
". Note however that look-ahead and look-behind are NOT the same thing. You cannot use this for look-behind.If you are looking for a "bar" that isn't preceded by a "
foo
",/(?!foo)bar/
will not do what you want. That's because the(?!foo)
is just saying that the next thing cannot be "foo
"--and it's not, it's a "bar", so "foobar
" will match. You would have to do something like/(?!foo)...bar/
for that. We say "like" because there's the case of your "bar
" not having three characters before it. You could cover that this way:/(?:(?!foo)...|^.{0,2})bar/
. Sometimes it's still easier just to say:
if (/bar/ && $` !~ /foo$/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With