Ruby Regex Error: incompatible encoding regexp mat

2019-01-18 09:12发布

问题:

I'm getting two errors, both revolving around encoding and both related.

The first error (technically, a warning) I get when starting up WEBrick:

/Users/USERNAME/example/config/initializers/bb-ruby.rb:54: warning: invalid Unicode Property \P: /\:\-?\P/

The line it's referring to is: /\:\-?\P/,

It's just a bit of regex, ultimately part of this block:

@@tags['Razzing'] = [
  /\:\-?\P/,
  '<img src="/assets/emoticons/razzing.png">',
  'Razzing',
  ':P',
  :razzing]

Then, I also get the following error when parsing some strings (presumably due to this same line)...

Encoding::CompatibilityError
incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

I'm running Ruby 1.9.2 and Rails 3.2.1.

回答1:

Your Regex is being "compiled" as ASCII-8BIT.

Just add the encoding declaration at the top of the file where the Regex is declared:

# encoding: utf-8

And you're done. Now, when Ruby is parsing your code, it will assume every literal you use (Regex, String, etc) is specified in UTF-8 encoding.

UPDATE: UTF-8 is now the default encoding for Ruby 2.0 and beyond.



回答2:

Ruby 2.0 Document

/Pattern/u - stand for UTF-8