Regex Lookahead, Lookbehind and Atomic Groups
I Found These Things in My Regex Body but I Haven't Got a Clue What I Can Use Them For. Does Somebody Have Examples So I Can Try to Understand How They Work?...
I found these things in my regex body but I haven't got a clue what I can use them for. Does somebody have examples so I can try to understand how they work?
(?!) - negative lookahead
(?=) - positive lookahead
(?<=) - positive lookbehind
(?<!) - negative lookbehind
(?>) - atomic group
5 Answers
Examples
Given the string foobarbarfoo:
bar(?=bar) finds the 1st bar ("bar" which has "bar" after it)
bar(?!bar) finds the 2nd bar ("bar" which does not have "bar" after it)
(?<=foo)bar finds the 1st bar ("bar" which has "foo" before it)
(?<!foo)bar finds the 2nd bar ("bar" which does not have "foo" before it)
You can also combine them:
(?<=foo)bar(?=bar) finds the 1st bar ("bar" with "foo" before it and "bar" after it)
Definitions
Must Read
Look ahead positive (?=)
Find expression A where expression B follows:
A(?=B)