user2450763
user2450763

Reputation: 41

Ruby 1.9 unicode escapes in Regexp

I just upgraded an old project to Ruby 1.9.3. I'm having a bunch of trouble with unicode strings. It boils down to:

p = "\\username"; "Any String".match(/#{p}/)

That works in 1.8, and returns nil as expected. However, in 1.9 it throws:

ArgumentError: invalid Unicode escape

I'm trying to match '\u' in a string. I thought the two backslashes will escape it from registering as a unicode.

What am I missing here?

Edit: Single quotes don't work too:

1.9.3p429 :002 > p = '\\username'; "Any String".match(/#{p}/)
ArgumentError: invalid Unicode escape
from (irb):2

Upvotes: 4

Views: 577

Answers (1)

Casper
Casper

Reputation: 34308

When you do /#{p}/ it means p will be interpreted as a regular expression. Since your p is now equal to \username, then this Regexp compilation will fail (since it IS an invalid Unicode escape sequence):

>> Regexp.new "\\username"
RegexpError: invalid Unicode escape: /\username/

I.e. doing /#{p}/ is equal to writing /\username/.

Therefore you have to escape p from any regular expressions so it will be interpreted correctly:

"Any String".match(/#{Regexp.escape(p)}/)

Or just:

"Any String".match(Regexp.escape(p))

Upvotes: 3

Related Questions