-2

How do I use regex to get what is inside of a ${} enclosed value such as:

Dont care about this. ${This is the thing I want regex to return} Not this either.

Should return:

This is the thing I want regex to return

I've tried \${ }$ the best I got messing around on regex101.com I'll be honest I have no Idea what I'm doing as far as regex goes

using on c++ but would also (if possible) like to use in geany text editor

R. Smyth
  • 147
  • 6

2 Answers2

3

I suggest \${[^}]*}. Note that $ have special meaning in regular expressions and need to be escaped with a \ to be read literary.

I use [^}]* instead of .* between the braces to avoid making a long match including the entire value of:

${Another} match, more then one ${on the same line}

[^}] means anything but }

1

What you want is matching the starting ${ and the ending } with any amount of characters in between: \$\{.*\}. The special part here is the .*, . means any character and * means the thing in front of it can be matched 0 or more times.

Since you want thre matched results, you might also want to wrap it in (): (\$\{.*\}). The parenthesis makes regex remember the stuff inside for later use. See this stackoverflow on how to get the results back: How to match multiple results using std::regex

hnOsmium0001
  • 84
  • 1
  • 1
  • 5