-2

I am trying to construct a regular expression that will match any Ruby string that contains string interpolation, such as:

"This will contain my #{string}"

I came up with:

/".*#{.*}.*"/

but the #{} is interpreted as interpolation. How can I express #{} literally?

5
  • 1
    Escape the curly braces. Commented Jul 2, 2016 at 16:14
  • This looks like an XY question. Commented Jul 2, 2016 at 16:29
  • Yeah, no idea what is really necessary. Perhaps, ideone.com/8tanIc? Commented Jul 2, 2016 at 16:50
  • @WiktorStribiżew escaping # symbol is one backslash less :) Commented Jul 2, 2016 at 17:02
  • Take a look at rubular.com if you'd like to try out your regular expression against a test string Commented Jul 2, 2016 at 17:29

2 Answers 2

0

You can use single quotes when assigning the string to avoid interpolation and express #{} literally:

my_template = 'This will contain my #{string}'
# => "This will contain my \#{string}"

Or alternatively you can use double quotes and escape the # symbol with a slash (as seen in the return value of the statement above).


There's also another interpolation technique described here which may be helpful:

greeting = 'hello %s, my name is %s!'
interpolated = greeting % ['Mike', 'John']
# => "hello Mike, my name is John!"

Then you can detect %s with your regex, but still retain the ability to interpolate easily & use the string as a template.

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

Comments

0

Description

#\{([^}]*)\}

Regular expression visualization

** To see the image better, simply right click the image and select view in new window

This regular expression will do the following:

  • Matches substrings surrounded by #{....} structure
  • Captures the substring inside the brackets into Capture Group 1
  • Does not work recursively

Example

Live Demo

https://regex101.com/r/vR6mU9/1

Sample text

This will contain my #{string}

Sample Matches

MATCH 1
Capture group 0: #{string}
Capture group 1: `string`

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  #                        '#'
----------------------------------------------------------------------
  \{                       '{'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^}]*                    any character except: '}' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \}                       '}'
----------------------------------------------------------------------

Comments

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.