Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore characters in preg_match_all output

Tags:

regex

php

match

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?

like image 733
sleepless_in_seattle Avatar asked Dec 14 '22 19:12

sleepless_in_seattle


1 Answers

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.

like image 84
Sam Avatar answered Dec 17 '22 10:12

Sam