In this article you will learn about Negative Lookahead and positive lookahead assertions in regular expressions, their syntax and usage with examples.
Regular Expression Lookahead assertions are very important in constructing a practical regex. They belong to a group called lookarounds which means looking around your match, i.e. the elements before it or the elements after it. Lookaround consists of lookahead and lookbehind assertions. And as the name shows it means simply checking the element after your match and making a decision. It is basically of two types.
If you want to learn Regex with Simple & Practical Examples, I will suggest you to see this simple and to the point Complete Regex Course with step by step approach & exercises. This video course teaches you the Logic and Philosophy of Regular Expressions from scratch to advanced level.
It is used if the next element to your match is as per requirement described in regex. It actually matches for the element or characters but after matching, gives it up and returns only if there is a match or no match hence that is why they are called assertions. They only assert if in a given test string the match with certain conditions is possible or not Yes or No.
These type of assertions match the item which may be a character, characters or a group after the current match. And return their results. If the certain given conditions are fulfilled they give a result.
They are of two types
i. Positive lookahead.
ii. Negative lookahead.
In this type the regex engine searches for a particular element which may be a character or characters or a group after the item matched. If that particular element is present then the regex declares the match as a match otherwise it simply rejects that match.
The syntax of positive is like this
/ match (?=element) /
if element follows match then it will be a match otherwise match will technically not be a match and will not be declared as a match. The positive lookahead is a sort of group with parenthesis around it. Within this group the expression starts with a question mark immediately followed by equal sign and then the element to look ahead.
Now we are going to discuss a more practical application lets suppose you want to match all those USD characters which are followed by some digits. Simply you want to match all those USD characters which are immediately followed by numbers for example you want to match
but you don't want to match
Now the regex for this will be
/ USD(?=\s\d+?,?\d+) / gm
Here first the engine will search for U after finding U upper case the engine will look for S if it finds it will see if it is immediately followed by D. In case of a USD match the engine will enter lookahead and finds that it is a positive lookahead and in this look ahead there is a space followed by an optional number one or more quantifier then an optional comma and after comma there is one or more digits. This regex will match all USD words followed by a number of one or more digits.
In this type of lookahead the regex engine searches for a particular element which may be a character or characters or a group after the item matched. If that particular element is not present then the regex declares the match as a match otherwise it simply rejects that match.
The syntax is
/ match(?!element) / gm
Where match is the item to match and element is the item which should not immediately follow for a successful match. The match will be declared a match if it is not followed by a given element. Thus this pattern helps in matching those items which have a condition of not being immediately followed by a certain character, group of characters or a regex group./ a(?!b) / will match all a not followed by b hence it will match