【发布时间】:2012-03-02 04:34:49
【问题描述】:
正如问题标题中所说的那样,是否有一种 Ruby 字符串上的方法等效于 String#Scan,但它不只返回每个匹配项的列表,而是返回一个 MatchDatas 的数组?例如:
# Matches a set of characters between underscore pairs
"foo _bar_ _baz_ hashbang".some_method(/_[^_]+_/) #=> [#<MatchData "_bar_"&rt, <MatchData "_baz_"&rt]
或者我可以得到相同或相似结果的任何方式都会很好。我想这样做是为了在 Ruby 字符串中找到“字符串”的位置和范围,例如"goodbye 和 "world" 在“'再见'残酷的'世界'”中。
【问题讨论】:
标签: ruby regex string parsing pattern-matching