Skip to content

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

Leave a Reply

Your email address will not be published.