#21422 [Opn->Bgs]: preg_match regexps not greedy by default
| From: | gerzson@php.net | Date: | Sun, 05 Jan 2003 10:01:04 +0000 |
| Subject: | #21422 [Opn->Bgs]: preg_match regexps not greedy by default | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-29091@lists.php.net to get a copy of this message | ||
ID: 21422
Updated by: gerzson@php.net
Reported By: astetson@aol.com
-Status: Open
+Status: Bogus
Bug Type: *Regular Expressions
Operating System: Linux
PHP Version: 4.3.0
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2003-01-05 01:37:07] astetson@aol.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!
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=21422&edit=1