views:

197

answers:

5

I need an regular expression which matches the following:

'"test foo bar", "test"' => no match
'"test, foo bar", "test"' => "test, foo bar"
'"test, foo bar"' => "test, foo bar"
'"test foo bar"' => no match
'"test", "test, foo"' => "test, foo"
'"test", ","' => no match
'"test", 0, 0, "foo"' => no match

the regex should match any strings which contains the comma

I started with \".+?\" but it selects until the next string, which is not applicable for me. Thank you

Edit: thank you, i figured it out with all of your help \"[\w]+?,.+?\"

+1  A: 

Have you tried:

"[^"]?,[^"]+?"

That will match, a ", any character not a " once or more until a comma, a comma, any character not a " once or more until a ", and a "

Vinko Vrsalovic
Sorry, maybe it was unclear, i want to have if this is my string: '"test, foo", "test", "bar"' -> "test, foo".
codedevour
+1  A: 
"[^,"]+?,[^"]+?"
gustafc
+1  A: 

Try this: \"[^\"]*,[^\"]*\".

NawaMan
this is nearly perfect, but it matches ",", how can i prevent this?
codedevour
@chrsk Change the *'s to +'s.
pst
+2  A: 

Do you absolutely have to do this in one regular expression? I would use one regex to split the string into its constituent parts, and then just iterate over those to find the first one with a comma.

I don't have Java on this laptop yet, so I can't check the code to do the splitting, but I'd expect the regex to be something like this:

("[^"]*")(?:, ("[^"]*"))*

It's possible that the Java regex engine doesn't let you get at multiple matches for the same capturing group, however... I'm pretty sure you could use that in .NET easily enough, but it might be slightly trickier in Java. You might need something like:

(?:^|, )("[^"]*")

to consume "either the start of a line, or a comma followed by a space" before the start of the actual string.

All of this is still just trying to split the text into its bits though - it's not trying to find the first string containing a comma... but it may be the starting point for doing so. Unfortunately at that point my regex-fu runs out - I'd still do it the "split and then iterate" way :)

Jon Skeet
Hey, thank you for your help, you're definetly right for a programmatically way, but i only wanted to do a ide find statement, which is easy and fast to reuse, where i can individually do some changes
codedevour
A: 

I figured it out with all of your help \"[\w]+?,.+?\"

codedevour