我正在使用Ruby的StringScanner來標準化一些英文文本。如何從StringScanner捕獲項目?
def normalize text
s = ''
ss = StringScanner.new text
while ! ss.eos? do
s += ' ' if ss.scan(/\s+/) # mutiple whitespace => single space
s += 'mice' if ss.scan(/\bmouses\b/) # mouses => mice
s += '' if ss.scan(/\bthe\b/) # remove 'the'
s += "#$1 #$2" if ss.scan(/(\d)(\w+)/) # should split 3blind => 3 blind
end
s
end
normalize("3blind the mouses") #=> should return "3 blind mice"
相反,我只是得到" mice"
。
StringScanner#scan
未捕獲(\d)
和(\w+)
。
回到你身邊! :-)不能相信我錯過了。 –
是的,在ruby文檔中很難看到''[]''。 – zhon