Reputation: 1224
I need to extract text in curly braces but only if the first word within them is a "allowed" word. for example the following text:
awesome text,
a new line {find this braces},
{find some more} in the next line.
Please {dont find} this ones.
in this simple example "find" stands for a allowed word
my attemp:
$pattern = '!{find(.*)}!is';
$matches = array();
preg_match_all( $pattern, $text, $matches, PREG_SET_ORDER );
returns a weird result (print_r):
Array
(
[0] => Array
(
[0] => {find this braces},
{find some more} in the next line.
Please {dont find}
[1] => this braces},
{find some more} in the next line.
Please {dont find
)
)
while working fine without the "find" in the pattern (but then the one with "dont" is found, too.
What may be the cause for this?
Upvotes: 0
Views: 61
Reputation: 32797
.*
would match greedily i.e as much as possible.Use .*?
to match lazily i.e as less as possible
So your regex would be
!{find(.*?)}!is
Alternatively you can use [^{}]
instead of .*?
..In that case you don't need to use singleline mode
!{find([^{}]*)}!i
Upvotes: 3