#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    199
    Rep Power
    43

    Arrow Regular Expressions are Eating Too Much


    Hey Guys,

    I am trying to get a relatively simple regular expression working. By default, regular expressions eat as much of the string as possible. In this case, however, I am trying to find the end of a link ("</a>"), so I want the regular expression to eat as little as possible. Normally, you can put a ? after the * to indicate that it should try for small matches, but PHP's eregi-replace is throwing me a REG_BADRPT error.

    String is something like this...
    "(<a .*</a>)" works, but eats too much
    "(<a .*?</a>)" throws an error
  2. #2
  3. Permanently Banned
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2006
    Location
    In a whale
    Posts
    4,147
    Rep Power
    0
    1) Post in the right forum

    2) Use preg_replace

    3) You want to find everything between <a and </a>? Including what's in the HTML?

    4) Regular expressions are extremely obese, please do not make fun them, guys.

    5) ???

    6) PROFIT!!!
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Using ? as an ungreedy modifier is a Perl/PCRE feature. It doesn't work with the ereg* functions.
    Originally Posted by ryon420
    2) Use preg_replace
    Quoted for emphasis.

    Comments on this post

    • SKDevelopment agrees : Thanks, I did not know that.
  6. #4
  7. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    I'm leaving this thread in the PHP forum since this appears to be less of a regular expression issue and more related to PHP functions.
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    791
    Rep Power
    237
    Since ereg_* functions are not binary safe, personally I am trying to avoid using them at all. Also the PHP manual states that PCRE are often faster. And I read somewhere POSIX regular expressions are going to to be moved to PECL in the future versions of PHP. I would advise not to use ereg_* functions if possible and use preg_* functions instead ...
    Last edited by SKDevelopment; October 10th, 2008 at 08:44 AM.

IMN logo majestic logo threadwatch logo seochat tools logo