php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21422 preg_match regexps not greedy by default
Submitted: 2003-01-05 01:37 UTC Modified: 2003-01-05 04:00 UTC
From: astetson at aol dot com Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 4.3.0 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
48 + 33 = ?
Subscribe to this entry?

 
 [2003-01-05 01:37 UTC] astetson at aol dot com
Perl regexps are greedy by default.  \d+ will get all decimals possible (\d+? will only find one - it's lazy).  It seems preg_match is not greedy by default.

code:
preg_match("/^\w+(\d+)$/", $change_link, $change_id);
print("My ID is $change_id[1]<BR>");

when passed: "edit11"
change_id[1] is equal to "1".  It should be equal to "11" especially when my regexp is looking for the end of the line ($) after it!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-05 04:00 UTC] gerzson@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Your script is the evidence that preg_* is greedy by default, because \w+ consumes as many characters as possible - letters and digits as well. The last digit is not used because \d+ must match at least one digit in order that the whole expression succeeds.

Ask for help on the mailing list to figure out the proper regexp.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Oct 02 06:05:52 2022 UTC