Ruby String#scan equivalent to return MatchData

后端 未结 3 1054
余生分开走
余生分开走 2021-02-08 03:18

As basically stated in the question title, is there a method on Ruby strings that is the equivalent to String#Scan but instead of returning just a list of each match, it would r

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 03:41

    memo = []
    "foo _bar_ _baz_ hashbang".scan(/_[^_]+_/) { memo << Regexp.last_match }
     => "foo _bar_ _baz_ hashbang"
    memo
     => [#, #]
    

提交回复
热议问题