Q
獲取紅寶石
12
A
回答
19
試試這個:
res = []
"abab".scan(/a/) do |c|
res << [c, $~.offset(0)[0]]
end
res.inspect # => [["a", 0], ["a", 2]]
1
讓我吃驚,沒有類似String#scan
這將返回MatchData
對象的數組,類似於String#match
任何方法。所以,如果你喜歡猴子打補丁,你可以與託德的解決方案結合本(Enumerator
在1.9中引入):
class Regexp
def scan str
Enumerator.new do |y|
str.scan(self) do
y << Regexp.last_match
end
end
end
end
#=> nil
/a/.scan('abab').map{|m| m.offset(0)[0]}
#=> [0, 2]
+0
NoMethodError:undefined method'scan'for – Andy 2016-01-07 12:49:43
6
有一個疑難雜症看出來這裏,這取決於你所期望的行爲。
如果您在"dadad"
搜索/dad/
你只能得到[["dad",0]]
因爲scan
進步到每場比賽結束的時候找到一個(這是我錯了)。
我想出了這樣的選擇:
def scan_str(str, pattern)
res = []
(0..str.length).each do |i|
res << [Regexp.last_match.to_s, i] if str[i..-1] =~ /^#{pattern}/
end
res
end
如果你想你也可以從標準庫中做類似的事情與StringScanner,它可能是長字符串更快。
4
非常相似,@jim說,並適用於較長的串好一點:
def matches str, pattern
arr = []
while (str && (m = str.match pattern))
offset = m.offset(0).first
arr << offset + (arr[-1] ? arr[-1] + 1 : 0)
str = str[(offset + 1)..-1]
end
arr
end
嗨 - 抱歉大量垃圾郵件,但http://area51.stackexchange.com/proposals/74083/korean-語言可以使用你,如果你不在那裏! – 2016-05-23 18:39:16