Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Unicode Character in Regular Expression

How can I create a ruby regular expression that includes a unicode character?

For example, I would like to the character "\u0002" in my regular expression.

like image 361
Tom Rossi Avatar asked Aug 11 '26 05:08

Tom Rossi


1 Answers

You can write /\x02/ :

"\u0002" =~ /\x02/
#=> 0

If you're not sure, you can just start from a string :

Regexp.new("\u0002")
#=> /\x02/

Here's another example :

"☀☁☂" =~ /\u2602/
#=> 2

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
like image 55
Eric Duminil Avatar answered Aug 12 '26 17:08

Eric Duminil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!