Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Regex Error: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

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.

like image 974
Shpigford Avatar asked Mar 25 '12 02:03

Shpigford


2 Answers

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.

like image 106
Fábio Batista Avatar answered Nov 01 '22 18:11

Fábio Batista


Ruby 2.0 Document

/Pattern/u - stand for UTF-8
like image 5
Nil Liu Avatar answered Nov 01 '22 16:11

Nil Liu