How the preg_match handles the delimiter when \Q..

2020-02-14 07:11发布

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!

4条回答
地球回转人心会变
2楼-- · 2020-02-14 07:45

\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.

preg_match('~\Q http:// \E~', ' http:// ', $match);
var_dump($match);

# => array(1) { [0]=> string(7) " http:// " }

Use preg_quote() instead of \Q \E if the delimiter may appear within \Q \E

$text = ' http:// ';

preg_match('/' . preg_quote($text, '/') . '/', $text, $match);
var_dump($match);

# => array(1) { [0]=> string(9) " http:// " }
查看更多
淡お忘
3楼-- · 2020-02-14 07:53

As the definition:

Character: \Q...\E 

Description: Matches the characters between \Q and \E literally, suppressing the meaning of special characters

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 '\'

查看更多
冷血范
4楼-- · 2020-02-14 07:55

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-14 08:11

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:

var_dump(preg_match('@\Q http:// \E@', ' http:// '));
查看更多
登录 后发表回答