Results 1 to 9 of 9

Thread: [RESOLVED] Simple RegEx

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Resolved [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.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question 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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    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.

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Simple RegEx

    This worked for me
    Code:
    (tall|(?i)er)
    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

  6. #6
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    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);
    
    ?>

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Re: Simple RegEx

    Hello,

    Thanks SambaNeko. Now this is working. Can you refer me some good online tutorials on RegEx ? Please help.

    Thanks & Regards.

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Simple RegEx

    this is a great website.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width