php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81060 The expression does not compile correctly
Submitted: 2021-05-20 17:07 UTC Modified: 2021-05-20 17:17 UTC
From: vklimin at 510710 dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 8.0.6 OS: Debian 10
Private report: No CVE-ID: None
 [2021-05-20 17:07 UTC] vklimin at 510710 dot com
Description:
------------
The expression does not compile correctly and generates a warning.

Test script:
---------------
var_dump( preg_match( '#\Q/\E#', '/' ) );
var_dump( preg_match( '/\Q/\E/', '/' ) );

Expected result:
----------------
int(1)
int(1)

Actual result:
--------------
int(1)
Warning:  preg_match(): Unknown modifier '\' in [...][...] on line 1
bool(false)

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-20 17:09 UTC] vklimin at 510710 dot com
-Package: PECL +Package: PCRE related
 [2021-05-20 17:09 UTC] vklimin at 510710 dot com
Changed the name of the corresponding package
 [2021-05-20 17:14 UTC] imsop@php.net
-Status: Open +Status: Not a bug
 [2021-05-20 17:14 UTC] imsop@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The first character of the regex determines the delimiter; any extra instances of that delimiter need to be escaped.

var_dump( preg_match( '/\Q\/\E/', '/' ) );
 [2021-05-20 17:17 UTC] vklimin at 510710 dot com
The proposed solution is not correct

var_dump( preg_match( '/\Q\/\E/', '/' ) );

Expected result:
----------------
int(1)

Actual result:
--------------
int(0)
 [2021-05-20 17:26 UTC] imsop@php.net
My apologies, I see that \Q and \E change the meaning of the escape there.

However, they *don't* change the handling of the delimiter, as that happens before the regex is even passed to PCRE.

In other words, PCRE receives the regex \Q/\E and the modifiers \E/ hence the error message.
 [2021-05-20 17:32 UTC] danack@php.net
HI vklimin,

While suprising, and possibly could be documented better, it appears that \Q and \E probably won't work for what you're trying to use them for: https://stackoverflow.com/a/20520551/778719

"Use preg_quote() instead of \Q \E if the delimiter may appear within \Q \E"
 [2021-05-20 17:42 UTC] imsop@php.net
The following pull request has been associated:

Patch Name: Clarify that \Q and \E don't affect delimiters
On GitHub:  https://github.com/php/doc-en/pull/611
Patch:      https://github.com/php/doc-en/pull/611.patch
 [2021-05-20 17:44 UTC] imsop@php.net
The key here is the term "metacharacters", which are the characters listed here: https://www.php.net/manual/en/regexp.reference.meta.php

I've proposed an edit to the manual to explicitly mention delimiters: https://github.com/php/doc-en/pull/611/files
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jan 27 07:03:34 2022 UTC