RegExp #matse只返回一个匹配项

sha*_*ote 4 ruby regex pcre

请解释一下,为什么match()只返回一个匹配,而不是四个(例如):

s = 'aaaa'
p /a/.match(s).to_a # => ["a"]
Run Code Online (Sandbox Code Playgroud)

奇怪的是,分组match()返回两个匹配,独立于真实匹配计数:

s = 'aaaa'
p /(a)/.match(s).to_a # => ["a", "a"]

s = 'a aaa a'
p /(a)/.match(s).to_a # => ["a", "a"]
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答.

Tim*_*ker 10

您需要使用.scan()多次匹配:

p s.scan(/a/).to_a
Run Code Online (Sandbox Code Playgroud)

通过分组,您可以获得一个总体匹配结果,每个组一个结果(使用时.match().两个结果在您的正则表达式中相同.

一些例子:

> /(a)/.matc­h(s).to_a
=> ["a", "a"]           # First: Group 0 (overall match), second: Group 1
> /(a)+/.mat­ch(s).to_a­
=> ["aaaa", "a"]        # Regex matches entire string, group 1 matches the last a
> s.scan(/a/­).to_a
=> ["a", "a", "a", "a"] # Four matches, no groups
> s.scan(/(a­)/).to_a
=> [["a"], ["a"], ["a"], ["a"]] # Four matches, each containing one group
> s.scan(/(a­)+/).to_a
=> [["a"]]              # One match, the last match of group 1 is retained
> s.scan(/(a­+)(a)/).to­_a
=> [["aaa", "a"]]       # First group matches aaa, second group matches final a
> s.scan(/(a­)(a)/).to_­a
=> [["a", "a"], ["a", "a"]] # Two matches, both group participate once per match
Run Code Online (Sandbox Code Playgroud)