Ruby正则表达式错误:不兼容的编码regexp匹配(ASCII-8BIT regexp与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时遇到的第一个错误(技术上, / p>
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]
$ b )...
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.
声明文件顶部的正则表达式声明:
Just add the encoding declaration at the top of the file where the Regex is declared:
# encoding: utf-8
完成了。现在,当Ruby解析你的代码时,它将假设你使用的每个文本(Regex,String等)以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
现在是Ruby 2.0及更高版本的默认编码。
UPDATE: UTF-8
is now the default encoding for Ruby 2.0 and beyond.
这篇关于Ruby正则表达式错误:不兼容的编码regexp匹配(ASCII-8BIT regexp与UTF-8字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!