note 24691 deleted from pcre.pattern.syntax by sniper

From: Date: Tue, 30 Sep 2003 12:20:42 +0000
Subject: note 24691 deleted from pcre.pattern.syntax by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57420@lists.php.net to get a copy of this message
Note Submitter: ---- This will match SOME quoted strings: '{("|\').*?\1}msi' However match may be incorrect with greedy regexps, if there are several strings on the same line. With ungreedy regexps, this may fail also if the string embeds some escaped quotes. It also fails if the string spans several lines (as allowed in PHP) by embedding newlines (typical also for string litterals in PHP code, containing HTML text where we try to match quoted strings). The correct and simple way to match any string (using a single-quoted litteral for the regexp within the PHP source code): '{(?>(\')|")(((?(2)[^\']|[^"])|(\\\\\\1))*)\\1}' Note how the escaped litteral backslash in the regexp (stored in the runtime regexp string as two backslashes) needs to be encoded as FOUR backslashes in the regexp string litteral, and that other simple regexp escapes are doubled, and the two occurences of a litteral single-quote within the regexp needs to be escaped in the single-quoted regexp litteral! So at runtime, the effective regexp string is: {(?>(')|")(((?(2)[^']|[^"])|(\\\1))*)\1} On match: \1 will be the surrounding quoting character used (the '(?>...)' pattern is matched only on first occurence, and not later when parsing the content, and this avoids rollbacking when the second quote is not found if a simpler '(...)' pattern was used), \2 will be set to a single quote, only if single-quoting was used, \3 will be the string content, \4 will be the last non-quote character of the content, if any, \5 will be an escaped quoting character, if present in the string.

« previous php.notes (#57420) next »