【发布时间】:2026-02-13 09:55:01
【问题描述】:
有没有一种快速的方法可以在 Ruby 中找到正则表达式的每个匹配项?我查看了 Ruby STL 中的 Regex 对象并在 Google 上搜索无济于事。
【问题讨论】:
-
我读到这是我如何在字符串中搜索所有正则表达式模式并且非常困惑......
有没有一种快速的方法可以在 Ruby 中找到正则表达式的每个匹配项?我查看了 Ruby STL 中的 Regex 对象并在 Google 上搜索无济于事。
【问题讨论】:
使用scan 应该可以解决问题:
string.scan(/regex/)
【讨论】:
/(?=(...))/。
要查找所有匹配的字符串,请使用 String 的 scan 方法。
str = "A 54mpl3 string w1th 7 numb3rs scatter36 ar0und"
str.scan(/\d+/)
#=> ["54", "3", "1", "7", "3", "36", "0"]
如果需要,MatchData,即 Regexp match 方法返回的对象的类型,请使用:
str.to_enum(:scan, /\d+/).map { Regexp.last_match }
#=> [#<MatchData "54">, #<MatchData "3">, #<MatchData "1">, #<MatchData "7">, #<MatchData "3">, #<MatchData "36">, #<MatchData "0">]
使用MatchData的好处是可以使用offset之类的方法:
match_datas = str.to_enum(:scan, /\d+/).map { Regexp.last_match }
match_datas[0].offset(0)
#=> [2, 4]
match_datas[1].offset(0)
#=> [7, 8]
如果您想了解更多信息,请查看以下问题:
阅读 Ruby 中的特殊变量 $&、$'、$1、$2 也会有所帮助。
【讨论】:
如果你有一个带有组的正则表达式:
str="A 54mpl3 string w1th 7 numbers scatter3r ar0und"
re=/(\d+)[m-t]/
您可以使用 String 的 scan 方法来查找匹配组:
str.scan re
#> [["54"], ["1"], ["3"]]
要找到匹配的模式:
str.to_enum(:scan,re).map {$&}
#> ["54m", "1t", "3r"]
【讨论】:
str.scan(/\d+[m-t]/) # => ["54m", "1t", "3r"] 比 str.to_enum(:scan,re).map {$&} 更惯用
/(\d+)[m-t]/ 不是/\d+[m-t]/ 要写:re = /(\d+)[m-t]/; str.scan(re) 是相同的str.scan(/(\d+)[mt]/) 但我得到#> [["" 54 "], [" 1 "], [" 3 "]] 而不是"54m", "1t", "3r"] 问题是:如果我有一个带有组的正则表达式,并且想在不更改正则表达式(离开组)的情况下捕获所有模式,我该怎么做?从这个意义上说,一个可能的解决方案是:str.to_enum(:scan,re).map {$&}
您可以使用string.scan(your_regex).flatten。如果您的正则表达式包含组,它将以单个普通数组返回。
string = "A 54mpl3 string w1th 7 numbers scatter3r ar0und"
your_regex = /(\d+)[m-t]/
string.scan(your_regex).flatten
=> ["54", "1", "3"]
正则表达式也可以是一个命名组。
string = 'group_photo.jpg'
regex = /\A(?<name>.*)\.(?<ext>.*)\z/
string.scan(regex).flatten
你也可以使用gsub,如果你想要MatchData,这只是另一种方式。
str.gsub(/\d/).map{ Regexp.last_match }
【讨论】:
your_regex = /(\d+)[m-t]/ 中删除分组,您将不需要使用flatten。您的最后一个示例使用last_match,在这种情况下它可能是安全的,但它是全局的,如果在调用last_match 之前匹配了任何正则表达式,则可能会被覆盖。相反,根据模式和需求,使用 string.match(regex).captures # => ["group_photo", "jpg"] 或 string.scan(/\d+/) # => ["54", "3", "1", "7", "3", "0"] 可能更安全,如其他答案所示。