是否有一種可接受的方式來處理Ruby 1.9中輸入的編碼未知的正則表達式?比方說,我的輸入恰好是UTF-16編碼:Ruby 1.9:輸入編碼未知的正則表達式
x = "foo<p>bar</p>baz"
y = x.encode('UTF-16LE')
re = /<p>(.*)<\/p>/
x.match(re)
=> #<MatchData "<p>bar</p>" 1:"bar">
y.match(re)
Encoding::CompatibilityError: incompatible encoding regexp match (US-ASCII regexp with UTF-16LE string)
我目前的做法是使用UTF-8內部和重新編碼(副本),如有必要,輸入:
if y.methods.include?(:encode) # Ruby 1.8 compatibility
if y.encoding.name != 'UTF-8'
y = y.encode('UTF-8')
end
end
y.match(/<p>(.*)<\/p>/u)
=> #<MatchData "<p>bar</p>" 1:"bar">
但是,這對我來說感覺有些尷尬,我想問問是否有更好的方法來做到這一點。
謝謝!對我來說,它並沒有發生,反過來編碼正則表達式。這確實快了很多! 對於其他嘗試這樣做的人:當您嘗試測試代碼時,請注意虛擬編碼('#dummy?')。花了我一段時間找出爲什麼它不工作。 – DataWraith 2009-12-22 09:57:20
同意表現 - 我發現它記憶正則表達式的指數更快。快速破解這裏來處理空白剝離:https://gist.github.com/mahemoff/c877eb1e955b1160dcdf6f4d4c0ba043 – mahemoff 2016-06-28 09:04:07