我遇到了红宝石正则表达式的问题。我需要找到所有(可能重叠的)匹配项。这是问题的简化:
#Simple example
"Hey".scan(/../)
=> ["He"]
#Actual results
#With overlapping matches the result should be
=> ["He"], ["ey"]
我试图执行并获得所有结果的正则表达式如下所示:
"aaaaaa".scan(/^(..+)\1+$/) #This looks for multiples of (here) "a" bigger than one that "fills" the entire string. "aa"*3 => true, "aaa"*2 => true. "aaaa"*1,5 => false.
=> [["aaa"]]
#With overlapping results this should be
=> [["aa"],["aaa"]]
是否有图书馆或方法可以在 ruby 中执行正则表达式以获得我想要的结果?
我发现了一些线索,表明这在 Perl 中是可能的,但经过数小时的研究,我没有发现任何关于 Ruby 方法的信息。
但是我能够找到这个“ Javascript Regex - Find all possible matches, even in already capture matches ”,但是我找不到任何与 Ruby 类似的东西,也找不到类似于 Ruby 版本中最后一个索引属性的东西。老实说,我认为它无论如何都不会起作用,因为我打算使用的正则表达式是递归的并且依赖于整个字符串,而该方法会切掉字符串。