Regex to get text BETWEEN two characters

Viewed 44079

PLEASE READ CAREFULLY: This is a bit unusual and you will be tempted to say things like "that isn't how to use a regex" or "dude, just use String.SubString()," Etc...

I have a need to write a regex (to use a pre-existing method) that will match text BETWEEN curly braces, BUT NOT the curly braces themselves.

For Example: "{MatchThisText}" AND "La la la {MatchThisText} la la la..."
Should Both Match: "MatchThisText"

Someone asked this exact question a year ago, and he got a bunch of solutions for regexes that WILL match the curly braces in addition to "MatchThisText," resulting in a match of "{MatchThisText}" which is not what he (or I) needed.

If someone can write a Regex that actually matches only the characters BETWEEN the curly braces, I'd really appreciate it. It should allow any ASCII values, and should stop the match at the FIRST closing bracket.

For Example: "{retailCategoryUrl}/{filters}"
Should Match: retailCategoryUrl and filters
But NOT Match: "retailCategoryUrl}/{filters" (Everything but the outer braces)

Hey, this is a really tricky one for me, so please forgive the question if this is trivial for some of you.

THANKS!

5 Answers

In Javascript you get an array with all matches. Here is an example that machtes text between css` and ` for machting template strings:

yourstring.match(/css`([^}]+).`/gmi)
Related