我正在使用 Ruby 的 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)
and (\w+)
。