开发者

Select string with comma (regex)

I need an regular expression which matches the following:

'"test foo bar", "test"' => no match
'"test, foo bar", "test"' => "test, foo bar"
开发者_Python百科'"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 contain the comma

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

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


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 :)


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 "


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜