Java Regex – positive and negative lookahead example shows how to write regular expression patterns to perform positive lookahead and negative lookahead in Java.
What is a lookahead regular expression?
Let’s first understand what is a lookahead expression. Let’s say you have a string “CatBirdCatDog” and you want to extract the string “Cat” which is followed by the string “Dog”. In a normal regular expression, I will write something like the code given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
String str = "CatBirdCatDog"; //we want to extract string "Cat" that is followed by a "Dog" Pattern pattern = Pattern.compile("CatDog"); Matcher matcher = pattern.matcher(str); if(matcher.find()) { System.out.println("Matched \"" + matcher.group() + "\""); System.out.println("Starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 |
Matched "CatDog" Starting from index 7 to index 12 |
As you can see from the output, we found the match but the regular expression also captured the string “Dog”. So instead of output “Cat”, we got output as “CatDog”.
This is where the lookahead regular expression is useful. The lookahead expression matches the subsequence to the right of the current position but does not include it in the match. As the name “lookahead” suggests, it just looks ahead to check the specified condition, it does not include the condition in the final match.
How to write positive lookahead regular expression in Java?
The positive lookahead expression is used when we want to find something that is followed by something else but we do not want that something else to be included in the match. For example, we want to match “X” that is followed by a “Y”, but we do not want “Y” to be included in the match, we just want to check.
Syntax of positive lookahead expression is as given below.
1 |
(?=RegEx) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
String str = "CatBirdCatDog"; //positive lookahead to find "Cat" that is followed by string "Dog" Pattern pattern = Pattern.compile("Cat(?=Dog)"); Matcher matcher = pattern.matcher(str); if(matcher.find()) { System.out.println("Matched \"" + matcher.group() + "\""); System.out.println("Starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 |
Matched "Cat" Starting from index 7 to index 9 |
As we can see from the output, this time the output only includes “Cat” not “CatDog”.
How to write negative lookahead regular expression in Java?
The negative lookahead expression is exactly the opposite of the positive lookahead expression. It is used when we want to find something that is not followed by something else. For example, we want to match “X” that is not followed by a “Y”.
Syntax of negative lookahead expression is as given below.
1 |
(?!RegEx) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
String str = "CatBirdCatDog"; //negative lookahead to find "Cat" that is NOT followed by string "Dog" Pattern pattern = Pattern.compile("Cat(?!Dog)"); Matcher matcher = pattern.matcher(str); if(matcher.find()) { System.out.println("Matched \"" + matcher.group() + "\""); System.out.println("Starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 |
Matched "Cat" Starting from index 0 to index 2 |
As we can see from the output, the regular expression found the string “Cat” that is not followed by the string “Dog”.
This example is a part of the Java Regular Expression Tutorial with Examples.
Please let me know your views in the comments section below.