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

  • SEARCH
  • Home
  • 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 604789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:04:35+00:00 2026-05-13T17:04:35+00:00

Is is possible to create/use a regular expression pattern in ruby that is based

  • 0

Is is possible to create/use a regular expression pattern in ruby that is based on the value of a variable name?

For instance, we all know we can do the following with Ruby strings:

str = "my string"
str2 = "This is #{str}" # => "This is my string"

I’d like to do the same thing with regular expressions:

var = "Value"
str = "a test Value"
str.gsub( /#{var}/, 'foo' ) # => "a test foo"

Obviously that doesn’t work as listed, I only put it there as an example to show what I’d like to do. I need to regexp match based on the value of a variable’s content.

  • 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-05-13T17:04:35+00:00Added an answer on May 13, 2026 at 5:04 pm

    The code you think doesn’t work, does:

    var = "Value"
    str = "a test Value"
    p str.gsub( /#{var}/, 'foo' )   # => "a test foo"
    

    Things get more interesting if var can contain regular expression meta-characters. If it does and you want those matacharacters to do what they usually do in a regular expression, then the same gsub will work:

    var = "Value|a|test"
    str = "a test Value"
    str.gsub( /#{var}/, 'foo' ) # => "foo foo foo"
    

    However, if your search string contains metacharacters and you do not want them interpreted as metacharacters, then use Regexp.escape like this:

    var = "*This*"
    str = "*This* is a string"
    p str.gsub( /#{Regexp.escape(var)}/, 'foo' )
    # => "foo is a string"
    

    Or just give gsub a string instead of a regular expression. In MRI >= 1.8.7, gsub will treat a string replacement argument as a plain string, not a regular expression:

    var = "*This*"
    str = "*This* is a string"
    p str.gsub(var, 'foo' ) # => "foo is a string"
    

    (It used to be that a string replacement argument to gsub was automatically converted to a regular expression. I know it was that way in 1.6. I don’t recall which version introduced the change).

    As noted in other answers, you can use Regexp.new as an alternative to interpolation:

    var = "*This*"
    str = "*This* is a string"
    p str.gsub(Regexp.new(Regexp.escape(var)), 'foo' )
    # => "foo is a string"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I know that it's possible to create scripts for use on spreadsheets using
Hi it'd like to know if it's at all possible create a parametric equalizer
I'd like to create a regular expression such that when I compare the a
is it possible to get a regular expression that do: find first occurence of
I just want to create a regular expression out of any possible string. var
Is It possible to create HTML5 Web Socket by use of ASP.NET/Web Form?
Is it possible to use javascript to populate multidimensional arrays. I want to create
In my web application, I create some framework that use to bind model data
Consider the following regular expression pattern = (0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d This pattertn represents date strings in
Is it possible to create and use a TClientDataSet inside an Object at runtime?

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.