Ruby 正则表达式错误:不兼容的编码正则表达式匹配(ASCII-8BIT 正则表达式与 UTF-8 字符串) [英] 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.
我在启动 WEBrick 时遇到的第一个错误(技术上是警告):
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/
它所指的那一行是:/:-?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)
我正在运行 Ruby 1.9.2 和 Rails 3.2.1.
I'm running Ruby 1.9.2 and Rails 3.2.1.
推荐答案
您的正则表达式正在编译"为 ASCII-8BIT.
Your Regex is being "compiled" as ASCII-8BIT.
只需在声明 Regex 的文件顶部添加编码声明:
Just add the encoding declaration at the top of the file where the Regex is declared:
# encoding: utf-8
你已经完成了.现在,当 Ruby 解析您的代码时,它会假设您使用的每个文字(正则表达式、字符串等)都以 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.
更新: UTF-8
现在是 Ruby 2.0 及更高版本的默认编码.
UPDATE: UTF-8
is now the default encoding for Ruby 2.0 and beyond.
这篇关于Ruby 正则表达式错误:不兼容的编码正则表达式匹配(ASCII-8BIT 正则表达式与 UTF-8 字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!