Supposed I have a string like this:
How to get {strong} and {bold} using regex? I've already tried:Code:bla bla {strong} bla bla bla {bold} bla bla
but the result is:Code:/\{.*\}/
Code:{strong} bla bla bla {bold}
| SitePoint Sponsor |
Supposed I have a string like this:
How to get {strong} and {bold} using regex? I've already tried:Code:bla bla {strong} bla bla bla {bold} bla bla
but the result is:Code:/\{.*\}/
Code:{strong} bla bla bla {bold}


/\{.*?\}/
ah okay, thanks... lol, why I missed that one
actually I'm working for something like:
so it's nested, and I need this result:Code:bla bla {{strong} bla} bla bla {bold} bla bla
can I only use regex? I'm thinking to create recursive functionCode:{{strong} bla} {strong} {bold}![]()





Simple iteration would be enough
PHP Code:$s = 'bla bla {{stro{ng}} bla} bla bla {b{o}ld}bla bla';
$re = '/\{([^{}]*)\}/e';
while(preg_match($re, $s))
$s = preg_replace($re, '$matches[]="$1"', $s);
print_r($matches);
That's cool simple solution stereofrog!
Darn... I'm too late seeing your reply here, I have already made a recursive function, it's more complicated lol.
Btw, I want to have '\{' and '\}' not being translated, how to tell regex to ignore that?
For example:
and the result is the same with above while '\{bla\}' is not selected.Code:bla \{bla\} {{strong} bla} bla bla {bold} bla bla
thanks.





To handle escapes, change bounding \{'s to (?<!\\\\)\{ , inner group should be something like ((\\\\.|[^{}])*).
ok thanks, will try it..
Bookmarks