views:

30

answers:

3

I have a string which looks like:

"some token","another token","yet another token","Necessary Token","KNOWN TOKEN","next to known token"

How to get "Necessary Token" with regular expression from the given string?

I tried the next expression:

\"(.+)\","KNOWN TOKEN"

but it matches the whole substring from the beginning to the "KNOWN TOKEN" part. I need to make it 'lazy' but I cannot manage how to achieve this (I tried to put question marks after the first group and inside it and it didn't work).

+2  A: 

Change it to "anything else than quotes":

\"([^\"]+)\","KNOWN TOKEN"

Your (.+) matches everything (quotes too) between your some token... until the ...Necessary Token. My ([^\"]+) may match only Necessary Token, so the previous quotes will be set to the initial quotes of Necessary Token.

Or, if your programming language allows it, use a good CSV-parser that will take care even of commas within quotes, find the KNOWN TOKEN and take the element before it.

eumiro
@eumiro: the problem is that your first example returns "some token","another token","yet another token","Necessary Token"
Roman
@Roman, then take the second one. Or use a CSV-Parser
eumiro
@eumiro: the second does work, bingo! Could you explain why is it work?
Roman
@Roman: see my edited answer
eumiro
@eumiro: oh, I got it.
Roman
+3  A: 
\"([^\"]+)\","KNOWN TOKEN"
joni
A: 

Alternatively you can also use:

.*\"(.+)\","KNOWN TOKEN"

Working link

codaddict