From: anoop dot john at zyxware dot com Operating system: Ubuntu Jaunty PHP version: 5.2.10 PHP Bug Type: *Regular Expressions Bug description: Regex does not match when text added to matching text
Description: ------------ I am using a complex regex pattern to match stock tickers in a piece of text. The pattern given below $pattern = '/\(((?i:\s*[a-z]*\s*[a-z]*\s*,)*\s*(?i:AMEX|NASDAQ|NasdaqGM|NasdaqGS|NYSE)\s*(?i:,\s*[a-z]*\s*[a-z]*\s*)*):\s*([A-Z]+)\s*;((?i:\s*[a-z]*\s*[a-z]*\s*,)*\s*(?i:AMEX|NASDAQ|NasdaqGM|NasdaqGS|NYSE)\s*(?i:,\s*[a-z]*\s*[a-z]*\s*)*):\s*([A-Z]+)\s*\)/'; should match (AMEX,NYSE, Swiss Exchange: CRX;Nasdaq: QTWW) and it does match it when the subject string is given alone. However when you prepend another particular string that does not match this pattern in front of this subject string the regex ceases to match the original portion of the string. The culprit string is given below. (Euronext, NASDAQ: CRXL; AMEX,NYSE,NASDAQ, Swiss Exchange: CRX;NasdaqGM: QTWW) The pattern matches only one opening brace and will not match another opening brace. So it cannot be that the pattern ate through the first pair of brackets and went into the second pair of brackets and fails to match when the culprit string is prepended. Reproduce code: --------------- $pattern = '/\(((?i:\s*[a-z]*\s*[a-z]*\s*,)*\s*(?i:AMEX|NASDAQ|NasdaqGM|NasdaqGS|NYSE)\s*(?i:,\s*[a-z]*\s*[a-z]*\s*)*):\s*([A-Z]+)\s*;((?i:\s*[a-z]*\s*[a-z]*\s*,)*\s*(?i:AMEX|NASDAQ|NasdaqGM|NasdaqGS|NYSE)\s*(?i:,\s*[a-z]*\s*[a-z]*\s*)*):\s*([A-Z]+)\s*\)/'; preg_match_all($pattern, '(Euronext, NASDAQ: CRXL; AMEX,NYSE,NASDAQ, Swiss Exchange: CRX;NasdaqGM: QTWW) (AMEX,NYSE, Swiss Exchange: CRX;Nasdaq: QTWW)', $matches, PREG_SET_ORDER); var_export($matches); echo "<br /><br />"; preg_match_all($pattern, '(AMEX,NYSE, Swiss Exchange: CRX;Nasdaq: QTWW)', $matches, PREG_SET_ORDER); var_export($matches); Expected result: ---------------- array ( 0 => array ( 0 => '(AMEX,NYSE, Swiss Exchange: CRX;Nasdaq: QTWW)', 1 => 'AMEX,NYSE, Swiss Exchange', 2 => 'CRX', 3 => 'Nasdaq', 4 => 'QTWW', ), ) array ( 0 => array ( 0 => '(AMEX,NYSE, Swiss Exchange: CRX;Nasdaq: QTWW)', 1 => 'AMEX,NYSE, Swiss Exchange', 2 => 'CRX', 3 => 'Nasdaq', 4 => 'QTWW', ), ) Actual result: -------------- array ( ) array ( 0 => array ( 0 => '(AMEX,NYSE, Swiss Exchange: CRX;Nasdaq: QTWW)', 1 => 'AMEX,NYSE, Swiss Exchange', 2 => 'CRX', 3 => 'Nasdaq', 4 => 'QTWW', ), ) -- Edit bug report at http://bugs.php.net/?id=49568&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49568&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49568&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49568&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=49568&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49568&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=49568&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=49568&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=49568&r=needscript Try newer version: http://bugs.php.net/fix.php?id=49568&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=49568&r=support Expected behavior: http://bugs.php.net/fix.php?id=49568&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=49568&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=49568&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=49568&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49568&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=49568&r=dst IIS Stability: http://bugs.php.net/fix.php?id=49568&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=49568&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=49568&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=49568&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=49568&r=mysqlcfg