I'm trying to find all quoted strings in a given text so that I can apply proper highlighting. I've tried various regex but can't seem to get the results I want.

String line = "a big \"yellow\" dog! And a \"purple\" bird!";
String[] tokens = line.split("\".+\"");

That cuts out everything from 'yellow' to 'purple'. Is there a way to set the regex so that after the first match is found it cant be part of subsequent matches?

Given the string above, what I need is to extract "yellow" and "purple" only, including their quotations.


[edit]
Ok, just about got it.
Pattern p = Pattern.compile("\".+?\"");

String test = "testing some stuff"+"some more stuff";

Works as expected there. But if the string contains escaped quotes:
\"
then it throws it off.

So how can I tell it to ignore escape characters?

if this is the text:
"dfa\"sdfasdf"+"asdfasd"

Then everything should match except the plus sign.
I've tried stating for no escape character before the quotes, but I may not have done this properly.
Pattern.compile("[^\\e](\".*?\")[^\\e]");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.