|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-09-16 19:27 UTC] php at clayst dot com
Description:
------------
A regular expression which involves some unnecessary extra loops in execution will give the proper result (a match) with a given input string but will fail to match when it should if the input is one character longer.
In the example, the pattern is intended to match a string which contains:
(1) An optional substring consisting of one or more groups of word characters separated by dashes, with the entire substring (if present) terminated by a period; followed by
(2) A required substring consisting of one or more groups of word characters separated by dashes.
The 'good' pattern shown does this properly. The 'bad' pattern incorrectly makes the dashes between substrings optional -- i.e. there's an extra '?" after the '-' in both portions of the pattern.
This error makes the pattern inefficient as there are many possible matching substrings, but I believe it should still match a simple alpha string. In fact it fails to match if the input string is more than 20 characters long.
Reproduce code:
---------------
<?php
$goodpattern = '/^((\w+(-\w+)*)\.)?(\w+(-\w+)*)$/';
$badpattern = '/^((\w+(-?\w+)*)\.)?(\w+(-?\w+)*)$/';
$string1 = str_repeat('a', 20);
$string2 = str_repeat('a', 21);
print('Good pattern, 20 characters: ' . preg_match($goodpattern, $string1) . "\n");
print('Good pattern, 21 characters: ' . preg_match($goodpattern, $string2) . "\n");
print('Bad pattern, 20 characters: ' . preg_match($badpattern, $string1) . "\n");
print('Bad pattern, 21 characters: ' . preg_match($badpattern, $string2) . "\n");
?>
Expected result:
----------------
All matches should return a 1 because they all match the given string.
Actual result:
--------------
The first three matches return a 1 but the last returns a 0:
Good pattern, 20 characters: 1
Good pattern, 21 characters: 1
Bad pattern, 20 characters: 1
Bad pattern, 21 characters: 0
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 23:00:02 2025 UTC |
Thank you for not searching the bug database before submitting yet another bogus bug report about this issue. Read about the limitations in PCRE library here: http://www.pcre.org/pcre.txt This is not PHP bug, just PCRE library's limitation. (also mentioned in the manual, if you had bothered reading it)