I have this regex:
preg_match_all('/{.*?}/', $html, $matches);
Which returns all strings that are written inside curly braces. The $matches variable contains the { and } characters also. How can I remove them?
I don't want to do:
if ($matches[0] == "{variable}")
And I don't want to add ( and ) characters to the regexp because I don't want to use:
preg_match_all('/{(.*?)}/', $html, $matches);
if ($matches[0][0] == "variable")
So is there a simpler way to remove the curly braces from the $matches within the regex?
In PCRE (PHP's implementation of regex), you can use lookarounds to do zero-length assertions. A lookbehind, (?<=...)
, will make sure that expression occurs behind the internal pointer. A lookahead, (?=...)
, will make sure that expression occurs ahead of the internal pointer. These can both be negated if need be: (?<!...)
or (?!...)
.
This brings us to this expression:
(?<={).*?(?=})
Demo
Implement it the same way:
preg_match_all('/(?<={).*?(?=})/', $html, $matches);
// $matches[0] = 'variable';
@CasimirEtHippolyte makes a good point. This is a great example of where a lazy dot-match-all is not necessary and will potentially decrease performance with backtracking. You can replace the .*?
with [^}]*
to match 0+ non-}
characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With