RegEx: Grabbing values between quotation marks

前端 未结 20 1236
暖寄归人
暖寄归人 2020-11-22 02:13

I have a value like this:

\"Foo Bar\" \"Another Value\" something else

What regex will return the

20条回答
  •  旧巷少年郎
    2020-11-22 02:35

    The pattern (["'])(?:(?=(\\?))\2.)*?\1 above does the job but I am concerned of its performances (it's not bad but could be better). Mine below it's ~20% faster.

    The pattern "(.*?)" is just incomplete. My advice for everyone reading this is just DON'T USE IT!!!

    For instance it cannot capture many strings (if needed I can provide an exhaustive test-case) like the one below:

    $string = 'How are you? I\'m fine, thank you';

    The rest of them are just as "good" as the one above.

    If you really care both about performance and precision then start with the one below:

    /(['"])((\\\1|.)*?)\1/gm

    In my tests it covered every string I met but if you find something that doesn't work I would gladly update it for you.

    Check my pattern in an online regex tester.

提交回复
热议问题