Java Regex – positive and negative lookbehind example shows how to write regular expression patterns to perform positive lookbehind and negative lookbehind in Java.
What is a lookbehind regular expression?
Let’s say you want to find something that is preceded by something else. For example, you have a string “152535” and you want to find the number “5” that is preceded by the number “3”. The normal regular expression pattern to find such a number will be like given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
String str = "152535"; //we want to extract number 5 that is preceded by a number 3 Pattern pattern = Pattern.compile("[3]5"); //or just 35 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 "35" Starting from index 4 to index 5 |
As you can see from the output, we found the number “5” that is preceded by the number “3”. The problem is the match also included the number “3”, so instead of just extracting “5”, it extracted “35”.
This is where we need to use the lookbehind expression instead of a regular pattern. The lookbehind expression looks at the left side of the current position to check for the condition but does not include the condition in the actual match.
How to write a positive lookbehind expression in Java?
The positive lookbehind expression is used when we want to find something that is preceded by something else, but we do not want that something else to be included in the match. In the above example, we wanted to extract “5” that is preceded by the number “3”, but only wanted “5” in the output, not “35”.
Syntax of writing a positive lookbehind is as given below.
1 |
(?<=RegEx) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
String str = "152535"; //positive lookbehind to find "5" that is preceded by "3" Pattern pattern = Pattern.compile("(?<=3)5"); 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 "5" Starting from index 5 to index 5 |
As you can see from the output, this time we matched only “5”, not “35”.
How to write a negative lookbehind expression in Java?
The negative lookbehind expression is similar to the positive lookbehind expression, but it is used when we want to find something that is not preceded by something else. For example, we want to extract the number “5” that is not preceded by the number “3”.
The syntax of writing a negative lookbehind expression is as follows.
1 |
(?<!RegEx) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
String str = "152535"; //negative lookbehind to find "5" that is NOT preceded by "3" Pattern pattern = Pattern.compile("(?<!3)5"); Matcher matcher = pattern.matcher(str); while(matcher.find()) { System.out.println("Matched \"" + matcher.group() + "\""); System.out.println("Starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 3 4 |
Matched "5" Starting from index 1 to index 1 Matched "5" Starting from index 3 to index 3 |
As you can see from the output, we found 2 instances of “5” in our string that is not preceded by the number “3”.
This example is a part of the Java Regular Expression Tutorial with Examples.
Please let me know your views in the comments section below.