Is there an accepted way to deal with regular expressions in Ruby 1.9 for which the encoding of the input is unknown? Let's say my input happens to be UTF-16 encoded:
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)
My current approach is to use UTF-8 internally and re-encode (a copy of) the input if necessary:
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">
However, this feels a little awkward to me, and I wanted to ask if there's a better way to do it.