Difference between two regex: “([^”]*)“ vs ”(.*?)"

為{幸葍}努か 提交于 2021-02-05 06:52:54

问题


I am learning about using cucumber's step defintion, which use regex. I came across the following different usages and would like to know if there's some material difference between the two approaches of capturing a group within a pair of double quotes:

approach one: "([^"]*)"

approach two: "(.*?)"

For example, consider a string input: 'the output should be "pass!"'. Both approaches would capture pass!. Are there inputs where two the approaches capture differently; or are they equivalent?

Thanks


回答1:


Well, in naked eye they look same. But slight different. Have a look on this example:

input:

a " regex
example is
here" please

Output for "([^"]*)":

 regex
example is
here

And, Output for "(.*?)" is empty.

.*? means any character except \n (0 or more times), and there has few newlines between the quotes("). If we use this in regex we need to give the regex engine a hint to use Multiline matching.




回答2:


"([^"]*)" will also capture newlines, so if you have

"Something
that goes on two lines"

then it will match it.

"(.*?)" does not span newlines, so it will not match that phrase.

Unless you use the single-line modifier (?s). In which case . will also include newline characters. The following expression: (?s)"(.*?)" would then match and capture.




回答3:


Difference between "(.*?)" and "([^"]*)"

It depends upon where this regex fragment appears within the larger context of the overall pattern. It also depends upon the target string that is being searched. For example, given the following input string:

'foo "quote1" bar "quote2"'

The expression: /"(.*?)"$/ (note the added end of string anchor) will match: "quote1" bar "quote2" but the /"([^"]*)"$/ expression will match: "quote2".

The dot will match a double quote if it has to to get a successful overall match.



来源:https://stackoverflow.com/questions/22287292/difference-between-two-regex-vs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!