2

I'm trying to build a regex from a string object, which happens to be stored in a variable.

The problem I'm facing is that escaped sequences (in the string) such "\d" doesn't make to the resulting regex.

Regexp.new("\d") => /d/

If I use single quotes, tough, it works flawless.

Regexp.new('\d') => /\d/

But, as my string is stored in a variable, I always get the double-quoted string.

Is there a way to turn a double-quoted string to single-quoted string, so that I could use in the Regexp constructor ?

(I'd like to use the string interpolation feature of the double quotes)

ex.:

email_pattern = "/[a-z]*\.com"
whole_pattern = "to: #{email_pattern}"
Regexp.new(whole_pattern)

For better readability, I'd like to avoid escaping escape characters.

"\\d"
1
  • Why don't you use single quotes for your innermost string (email_pattern) and double quotes for the enclosing strings (whole_pattern)? Commented Nov 6, 2012 at 15:18

2 Answers 2

4

The problem is, that you end up with completely different strings, depending on whether you use single or double quotes:

"\d".chars.to_a
#=> ["d"]

'\d'.chars.to_a
#=> ["\\", "d"]

so when you are using double quotes, the single \ is immediately lost and cannot be recovered by definition, for example:

"\d" == "d"
#=> true

so you can never know what the string contained before the escaping took place. As @FrankSchmitt suggested, use the double backslash or stick with single quotes. There's no other way.

There's an option, though. You can define your regex parts as regexes themselves, instead of strings. They behave exactly as expected:

regex1 = /\d/
#=> /\d/

regex2 = /foobar/
#=> /foobar/

Then, you can build your final regex with #{}-style interpolation, instead of building the regex source from strings:

regex3 = /#{regex1} #{regex2}/
#=> /(?-mix:\d) (?-mix:foobar)/

Reflecting your example this would translate to:

email_regex = /[a-z]*\.com/
whole_regex = /to: #{email_regex}/
#=> /to: (?-mix:[a-z]*\.com)/

You may also find Regexp#escape interesting. (see the docs)

If you run into further escaping problems (with the slashes), you can also use the alternative Regexp literal syntax with %r{<your regex here>}, in which you do not need to escape the / character. For example:

%r{/}
#=> /\//

There's no getting around escaping the backslash \ with \\, though.

Sign up to request clarification or add additional context in comments.

Comments

0

Either create your string with single quotes:

 s = '\d'
 r = Regexp.new(s)

or quote the backslash:

 s = "\\d"
 r = Regexp.new(s)

Both should work.

2 Comments

Thanks for your reply. I've considered those suggestions, but I'd like to avoid escaping the escape character. I feel it get unreadable as soon as the regex grows.
Can you create the string with single quotes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.