ID: 49568 Updated by: j...@php.net Reported By: anoop dot john at zyxware dot com -Status: Open +Status: Feedback Bug Type: PCRE related Operating System: Ubuntu Jaunty PHP Version: 5.2.10 New Comment:
And you're 100% sure your pattern is not buggy? Previous Comments: ------------------------------------------------------------------------ [2009-09-16 01:39:22] anoop dot john at zyxware dot com 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 this bug report at http://bugs.php.net/?id=49568&edit=1