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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: astetson at aol dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC