Java RegEx – Extract Text between Quotes

Java RegEx – Extract Text between Quotes example shows how to extract text between quotes (double quotes or single quotes) using Java regex pattern.

How to extract text data between double quotes using Java regex pattern?

While parsing string data using regex, we often come across words wrapped inside double quotes like given below.

If you want to extract the text between double quotes, you can use the below-given regex pattern.

Where,

The meaning of this whole pattern is “a double quote, followed by any character that is not a double quote – zero or more times, followed by the ending double quote”.

Let’s try this pattern in a code example.

Output

As you can see from the output, the pattern extracted the data along with the double quotes as well. If you want to remove that, you can do that by using a regex group as given below.

Output

How to extract data between single quotes?

It can be done using basically the same pattern. Just that we do not need to escape the single quotes. Have a look at the example.

Output

If you want to learn more about regex patterns, please visit the Java regex tutorial.

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 *