Regular Expression Help

interclubs used Ask the Experts™
I'm using the following regex to replace text in between curly brackets {}:
preg_replace("/\\{(\w)\\}/e", '$mappedFields[\1]', $template);

But I need to allow underscores and hyphens _-. How would I change it to allow for multiple hypens and underscores, like {__some-key--here}

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Most Valuable Expert 2011
Top Expert 2016

Please post a sample of the test data you're using.  Where do you need to allow the underscores and hyphens?  In the pattern?  Or in the input strings?  Or in the output?  If we can see the test data and expected results, it will be easy to get you a good answer very quickly.  Thanks, ~Ray


Here is a sample of the data I am trying to match:


The ones that start with meta are the ones that seem to fail with the current regex.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
ozo gave you the updated pattern. The PHP version would be:

preg_replace("/\\{([-\\w]+)\\}/e", '$mappedFields[\1]', $template);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial