Reputation: 1439
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
Upvotes: 3
Views: 9740
Reputation: 34435
"(.*?)"
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.
Upvotes: 3
Reputation: 4069
"([^"]*)"
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.
Upvotes: 3
Reputation: 39443
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.
Upvotes: 4