开发者

How to get the nearest and the smallest token before the known one?

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 be开发者_开发问答ginning 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).


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.


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


Alternatively you can also use:

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

Working link

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜