正则表达式在第一次出现时停止

I have a string like so {!Hello} there, {!World}. I need to match {!Hello} and {!World} and I have my regex like so

$text = '{!Hello} there, {!World}';
preg_match_all("/\{\!.+\}/",$text,$var);

The problem is it matches the whole thing from beginning to end. That obviously means it's not stopping at the first occurrence of } but rather the last one. How can I match those?

EDIT: Forgot to mention I can't use \w+ because the word in between the brackets may contain special characters. IT may be something like {!Email__email@some.domain__IsInvalid}. I need to match this whole thing

You basically need to match ungreedy:
/\{\!.+?\}/

$text = '{!Hello} there, {!World} foo {!Email__someemail@some.domain__IsInvalid}';
preg_match_all("/\{\!.+?\}/",$text,$var);
print_r($var);

You could also add the U modifier (note it's uppercase):
/\{\!.+\}/U

$text = '{!Hello} there, {!World} foo {!Email__someemail@some.domain__IsInvalid}';
preg_match_all("/\{\!.+\}/U",$text,$var);
print_r($var);

http://phpfiddle.org/lite/code/31z-rtu

preg_match_all("/\{\!.+?}/",$text,$var);

I misplaced the ? operator first. PHP understands the ? operator, it does exactly what you ask for, it limits the matches to the first occurence.

Hih.

What you need is: /\{\![^\}]*\}/

It looks for characteres that are not a }, between {! and }

Try this regex:

`\{(\![^\{]+)\}`

In your pattern .+ is a greedy and it will match as much as possible. For example:

In examples:

{!Hello} there, {!World}}

{!Hello} there, {!World}} dsad

bold part are the matches found by your regex.

In my pattern:

[^\{] this part is also present with + which means greedy but it i have restricted it match till the { due to ^ sign in character class.

Hope it will help you!