我想在一个给定的字符串中找到所有匹配项,包括重叠的匹配项。我该如何实现?
# Example
"a-b-c-d".???(/\w-\w/) # => ["a-b", "b-c", "c-d"] expected
# Solution without overlapped results
"a-b-c-d".scan(/\w-\w/) # => ["a-b", "c-d"], but "b-c" is missing
"abaca".scan(/(?=(\w)(?:(?!\1)(\w))\1)/) #=> [["a", "b"], ["a", "c"]]
。 - sschmeck"a-b-c-d".scan(/(?<=(\w-\w))/).flatten
。 - Cary Swovelandinspect
来在在线演示中打印演示结果,没有人告诉你在实际代码中使用inspect
。我将其删除以避免进一步的混淆。 - Wiktor Stribiżew