-
[RESOLVED] Simple RegEx
Hello,
Please let me know whats wrong with the following RegEx. I am trying to get 'tall' and 'taller' from the $str. But I am getting only 'tall'. Please help.
PHP Code:
<?php
$str = "Jack is a tall man but his brother is taller than him";
$pattern = "/tall(er)*/";
preg_match($pattern, $str, $matches);
print_r($matches);
?>
Thanks a lot in advance.
Thanks & Regards.
-
Re: Simple RegEx
Code:
$pattern = "/tall(?:er)?/";
-
Re: Simple RegEx
Hello,
Thnaks danasegarane for the reply. But this is not working in the PHP. There is no change in the result. I need help.
Thanks & Regards.
-
Re: Simple RegEx
But when I am using online RegEx tool http://gskinner.com/RegExr/ and check the global option, it will mark both 'tall' and 'taller' with either of the syntaxes (danasegarane and myself, as explained above). Is there anything wrong with my configuration ? I become confused. Please help.
-
Re: Simple RegEx
This worked for me
Edit:
I don't have editor. So I tested here
-
Re: Simple RegEx
Your main problem here is that you're using preg_match(), which stops searching after it finds its first match, as opposed to preg_match_all(), which matches everything it can find (a "global match" - which is why that regex tester was working for you). Your pattern is also a little off. Give this a shot:
Code:
<?php
$str = "Jack is a tall man but his brother is taller than him";
$pattern = "/tall(er)?/";
preg_match_all($pattern, $str, $matches);
print_r($matches);
?>
-
Re: Simple RegEx
Hello,
Thanks SambaNeko. Now this is working. Can you refer me some good online tutorials on RegEx ? Please help.
Thanks & Regards.
-
Re: Simple RegEx
-
Re: Simple RegEx
Hi All,
Thanks a lot to you all for your kind help to finalize the subject.
Thanks & Regards.