php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81439 Please support the /n pattern modifier (PCRE2_NO_AUTO_CAPTURE)
Submitted: 2021-09-15 07:18 UTC Modified: 2021-10-15 15:12 UTC
From: roy-orbison at devo dot net dot au Assigned:
Status: Open Package: PCRE related
PHP Version: 8.0.10 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: roy-orbison at devo dot net dot au
New email:
PHP Version: OS:

 

 [2021-09-15 07:18 UTC] roy-orbison at devo dot net dot au
Description:
------------
PCRE has supported Perl's /n modifier since Version 10.30 14-August-2017, and has supported its behaviour via a NO_AUTO_CAPTURE option for many years before that.

It prevents numbered captures appearing in result sets, allowing only named groups through (except 0, which corresponds to Perl's $&). It can make complex patterns more readable, and debugging/output of matches smaller, more semantic, and better suited to iteration.


Test script:
---------------
<?php
preg_match('/(?:a|b)c(?:d|e)/', 'ace', $m1);
preg_match('/(a|b)c(d|e)/n', 'ace', $m2);
preg_match('/(?<prefix>foo|bar)baz(?<suffix>qux)?/', 'foobazqux', $m3);
preg_match('/(?<prefix>foo|bar)baz(?<suffix>qux)?/n', 'foobazqux', $m4);
var_dump($m1, $m2, $m3, $m4);

Expected result:
----------------
array(1) {
  [0]=>
  string(2) "ace"
}
array(1) {
  [0]=>
  string(2) "ace"
}
array(5) {
  [0]=>
  string(9) "foobazqux"
  ["prefix"]=>
  string(3) "foo"
  [1]=>
  string(3) "foo"
  ["suffix"]=>
  string(3) "qux"
  [2]=>
  string(3) "qux"
}
array(5) {
  [0]=>
  string(9) "foobazqux"
  ["prefix"]=>
  string(3) "foo"
  ["suffix"]=>
  string(3) "qux"
}


Actual result:
--------------
Warning: preg_match(): Unknown modifier 'n' in pcre-n.php on line 3

Warning: preg_match(): Unknown modifier 'n' in pcre-n.php on line 5
array(1) {
  [0]=>
  string(2) "ac"
}
NULL
array(5) {
  [0]=>
  string(9) "foobazqux"
  ["prefix"]=>
  string(3) "foo"
  [1]=>
  string(3) "foo"
  ["suffix"]=>
  string(3) "qux"
  [2]=>
  string(3) "qux"
}
NULL

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-15 15:12 UTC] felipe@php.net
The following pull request has been associated:

Patch Name: ext/pcre - enable /n modifier
On GitHub:  https://github.com/php/php-src/pull/7583
Patch:      https://github.com/php/php-src/pull/7583.patch
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Oct 25 08:03:33 2021 UTC