Find overlapping Regexp matches

后端 未结 2 417
生来不讨喜
生来不讨喜 2021-01-05 04:53

I want to find all matches within a given string including overlapping matches. How could I achieve it?

# Example
\"a-b-c-d\".???(/\\w-\\w/)  # => [\"a-b\         


        
2条回答
  •  Happy的楠姐
    2021-01-05 05:36

    Use capturing inside a positive lookahead:

    "a-b-c-d".scan(/(?=(\w-\w))/).flatten
     # => ["a-b", "b-c", "c-d"]
    

    See Ruby demo

提交回复
热议问题