Need help with regular expressions (php)

For example, I have the following code:

$string  = "adf  gggg  eere value aaaa bbb (10) value 
ddttt ggg www (20) value ddttt ggg www dddd (40) ";
preg_match("/(value).*(\(\d+\))/is", $string, $result);
var_dump($result[2]); // outputs 40.

I am trying to get the first value (10). The above code outputs 40, which makes sense, but not what I want. String pattern: the word "value", then the number of any characters, then "(", the integer ")". It seems that I am missing something obvious ... I have not worked too much with regular expressions, but I believe that it can be somehow solved with help ?<!valueuntil it succeeds.

Thanks for any help.

+3
source share
3 answers

.* , , .*?, , :

/(value).*?(\(\d+\))/
+3

, . * .

preg_match("#value.*?\((\d+)\)#is", $string, $result);

, :

preg_match("#value[^(]+\((\d+)\)#is", $string, $result);
+2
.*?value.*?\((\d+)\).*

To be *? reluctant match.

+1
source

All Articles