I'm playing with regular expressions and I tried the \Q..\E escape sequence.
First try:
$regex = '/\Q http:// \E/';
var_dump(preg_match($regex, ' http:// '));
It tells me that '\' is unknown modifier, completely understandable.
Second try:
$regex = '/\Q http:\/\/ \E/';
var_dump(preg_match($regex, ' http:// '));
var_dump(preg_match($regex, ' http:\/\/ '));
It runs, not match the first string, but match the second one.
I know that I could use other delimiter character or solve it without \Q..\E, but I'm curious that how it works.
I through that at first it separates the regex from the modifiers by the delimiter (with handling the escaping if necessary) and after that the regex engine interprets the \Q..\E, but it seems like that when the \Q involved, then it not handles the escaped delimiter the same way.
What happens exactly at this case?
Thanks!
\Q
and\E
can be used to ignore regular expression metacharacters in the pattern.If the literal string contains the delimiter
/
for example, the regular expression compile fails or the match fails because it tries to match the escape character used to escape the delimiter. Delimiters that are between\Q \E
should be treated as literal characters, not delimiters.Use
preg_quote()
instead of\Q \E
if the delimiter may appear within\Q \E
As the definition:
Special characters as stated here and here are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
Each non-alphanumeric, non-backslash, non-whitespace character that's used as delimiter is not considered as special characters class but just delimiters, so we will find that:
var_dump(preg_match("+\Q+\E+", "+"));
will throw a similar error:
Warning: preg_match(): Unknown modifier '\'
Why the
\Q...\E
syntax doesn't support the delimiter?Since the
\E
is optional, the only way to know where the pattern ends is the delimiter. This is the reason why it is an exception with the\Q..\E
syntax.The regex delimiter (/ in this case) appears to be handled weirdly inside the literal string (\Q ... \E). It's a delimiter, so you need to escape it so preg can parse out the regex from the options. But it appears this does not stop \Q and \E from doing their job of interpreting the backslash slash sequence as a literal, rather than an escape.
If you use a different delimiter, things work as expected: