Regular Expression Help

interclubs
interclubs used Ask the Experts™
on
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}

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
([-\w]*)
Most Valuable Expert 2011
Top Expert 2016

Commented:
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

Author

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

    {to_ping}
    {pinged}
    {post_modified}
    {post_modified_gmt}
    {post_content_filtered}
    {post_parent}
    {guid}
    {menu_order}
    {post_type}
    {post_mime_type}
    {comment_count}
    {filter}
    {post_thumbnail}
    {meta__edit_last}
    {meta__edit_lock}
    {meta_downloaded}
    {meta_wpcf-downloadlink}
    {meta_wpcf-thirdparty-download-link}
    {meta_wpcf-thirdparty-featured-tool}
    {meta_wpcf-thirdparty-youtube-video-url}
    {meta_wpcf-thirdparty-website-url}
    {meta__thumbnail_id}

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
Commented:
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