|
-
Jul 14th, 2009, 02:21 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Jul 14th, 2009, 02:33 AM
#2
Re: Simple RegEx
Code:
$pattern = "/tall(?:er)?/";
Last edited by danasegarane; Jul 14th, 2009 at 02:38 AM.
Please mark you thread resolved using the Thread Tools as shown
-
Jul 14th, 2009, 05:05 AM
#3
Thread Starter
Hyperactive Member
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.
-
Jul 14th, 2009, 05:18 AM
#4
Thread Starter
Hyperactive Member
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.
-
Jul 14th, 2009, 06:03 AM
#5
Re: Simple RegEx
This worked for me
Edit:
I don't have editor. So I tested here
Last edited by danasegarane; Jul 14th, 2009 at 06:06 AM.
Please mark you thread resolved using the Thread Tools as shown
-
Jul 14th, 2009, 09:23 AM
#6
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);
?>
-
Jul 15th, 2009, 12:04 AM
#7
Thread Starter
Hyperactive Member
Re: Simple RegEx
Hello,
Thanks SambaNeko. Now this is working. Can you refer me some good online tutorials on RegEx ? Please help.
Thanks & Regards.
-
Jul 15th, 2009, 03:56 AM
#8
-
Jul 22nd, 2009, 01:19 PM
#9
Thread Starter
Hyperactive Member
Re: Simple RegEx
Hi All,
Thanks a lot to you all for your kind help to finalize the subject.
Thanks & Regards.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|