Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:03:49+00:00 2026-06-13T22:03:49+00:00

I’m trying to build a regex from a string object, which happens to be

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:03:50+00:00Added an answer on June 13, 2026 at 10:03 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.