在正则表达式中指定 Unicode 字符 [英] Specify Unicode Character in Regular Expression
本文介绍了在正则表达式中指定 Unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建包含 unicode 字符的 ruby 正则表达式?
How can I create a ruby regular expression that includes a unicode character?
例如,我想在我的正则表达式中使用字符\u0002".
For example, I would like to the character "\u0002" in my regular expression.
推荐答案
你可以写 /\x02/
:
"\u0002" =~ /\x02/
#=> 0
如果你不确定,你可以从一个字符串开始:
If you're not sure, you can just start from a string :
Regexp.new("\u0002")
#=> /\x02/
这是另一个例子:
"☀☁☂" =~ /\u2602/
#=> 2
正如@TomLord 在评论中提到的,您还可以指定一个范围.要检查字符串是否包含 UTF-8 箭头:
As mentionned by @TomLord in the comments, you can also specify a range. To check if a string includes a UTF-8 arrow :
"↹" =~ /[\u2190-\u21FF]/
#=> 0
这篇关于在正则表达式中指定 Unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文