How can I match a quote-delimited string with a regex?

前端 未结 9 741
一生所求
一生所求 2020-12-01 05:05

If I\'m trying to match a quote-delimited string with a regex, which of the following is \"better\" (where \"better\" means both more efficient and less likely to do somethi

9条回答
  •  没有蜡笔的小新
    2020-12-01 05:45

    From a performance perspective (extremely heavy, long-running loop over long strings), I could imagine that

    "[^"]*"
    

    is faster than

    ".*?"
    

    because the latter would do an additional check for each step: peeking at the next character. The former would be able to mindlessly roll over the string.

    As I said, in real-world scenarios this would hardly be noticeable. Therefore I would go with number two (if my current regex flavor supports it, that is) because it is much more readable. Otherwise with number one, of course.

提交回复
热议问题