Join the DZone community and get the full member experience.Join For Free
The Agile Zone is brought to you in partnership with Techtown Training. Learn how DevOps and SAFe® can be used either separately or in unison as a way to make your organization more efficient, more effective, and more successful in our SAFe® vs DevOps eBook.
Look-behind is one of those advanced/obscure regular expression features that I don’t use frequently enough to remember the syntax, but just frequently enough that I wish I could remember it.
Look-behind can be positive or negative. Look-behind says “match this position only if the preceding text matches (does not match) the following pattern.”
The syntax in Perl and similar regular expression implementations is
(?<= … ) for positive look-behind and
(?<! … ) for negative look-behind. For the longest time I couldn’t remember whether the next symbol after
? was the direction (i.e.
< for behind) or the polarity (
= for positive,
! for negative). I was more likely to guess wrong unless I’d used the syntax recently.
The reason I was tempted to get these wrong is that I thought “positive look-behind” and “negative look-behind.” That’s how these patterns are described. But this means the words and symbols come in a different order. If you think look-behind positive and look-behind negative then the words and the symbols come in the same order:
Maybe this syntax comes more naturally to people who speak French and other languages where adjectives follow the thing they describe. English word order was tripping me up.
By the way, the syntax for look-ahead patterns is simpler: just leave out the
<. The default direction for look-around patterns is forward. You don’t have to remember whether the symbol for direction or parity comes first because there is no symbol for direction.
Published at DZone with permission of John Cook , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.