TAGS :Viewed: 17 - Published at: a few seconds ago

[ Regular Expressions: Sherlock’s Secret Power ]

6. Lookbehind and lookahead regex lookaround Negative lookahead : It helps to get all matches not followed by given pattern. A(?!B) In below example we are trying to fetch “The” if it is not followed by “day”. Try it. negative lookahead. Positive lookahead: Simply put, Find expression A where expression B follows : A(?=B) In below example we are trying to fetch “The” if “day” follows it. Try it. positive lookahead Positive lookbehind: Simply put, Find expression A where expression B precedes: (?<=B)A In below example we are trying to “day” or “boy” if exists after “The/the”. Try it. positive lookbehind Negative lookbehind: Simply put, Find expression A where expression B does not precede: (?.*)|b*)[ac]/ bbabbbabbbbc ^ -- Start matching. Look at first item in alternation: (?>.*) bbabbbabbbbc ^ -- First match of .*, due to greedy quantifier -- The atomic grouping will disallow .* to be backtracked and rematched bbabbbabbbbc X -- [ac] cannot match -- Backtrack to () -- (?>.*) is atomic, check the next possibility by alternation: b* bbabbbabbbbc ^ -- Starting to rematch with b* bbabbbabbbbc ^ -- First match with b*, due to greedy quantifier bbabbbabbbbc ^ -- [ac] matches, end of regex, a match is found So if we use pattern as /((?>.*))[ac]/ There will be no match for the string bbabbbabbbbc Try it. use of the atomic group. While it would work if we allow backtracking. Try it.