views:

3296

answers:

3

I need a RegExp which matches a word or multiple words in quotes.

[\w]* matches a word

"[\w\W&&[^"]]*" matches multiple words in quotes.

(btw, not sure why \w\W works, but not a simple . (which should match all characters)

So how do i combine these two regexp?

+2  A: 

Does "[^"]+" do what you want? (Match a double-quote, match one or more chars that are not double quotes, then match a second double-quote.)

genehack
A: 

I would prefer:

"\s*((?:\w(?!\s+")+|\s(?!\s*"))+\w)\s*"

rather than "[^"]+" because ^" matches everything, no just \w (alphanumerical) char [a-zA-Z_0-9]

That way, you match only \w chars within quotes, without trailing spaces.

" ee eee e ee  "

gives you in group(1):

ee eee e ee

The negative look-ahead (?!\s+") are here to make sure I do not include the last spaces before the double quote;

VonC
+2  A: 

first of all thanks.

your answers really helped - but i noticed my question maybe wasn't clear enough.

Anyway, from your answers i ended up with this regexp:

"[^"]+"|[\w]+

Which matches Words and Multiple Words in Quotes.

e.g.: what is "this thing" will give 3 matches: first match: what second match: is third match: "this thing"

Which is exactly what i needed. Thanks again.

JP-Ulm
+1 Much simpler ;) I did not get at first what you wanted, but I leave my answer for reference
VonC