Java RegEx Positive and Negative Lookbehind

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.

Output

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.

Example:

Output

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.

Example:

Output

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.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *