php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73236 preg_match subcaptures are overwritten in repeating patterns
Submitted: 2016-10-03 18:19 UTC Modified: 2016-10-03 20:22 UTC
From: kael dot shipman at gmail dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 7.0.11 OS: Ubuntu Studio Linux 16.04.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kael dot shipman at gmail dot com
New email:
PHP Version: OS:

 

 [2016-10-03 18:19 UTC] kael dot shipman at gmail dot com
Description:
------------
It appears as though when a subcapture is contained within a repeating pattern, the value is overwritten by subsequent matches, leaving only the last captured value in the $matches array, rather than all matched values.

Test script:
---------------
preg_match(
  '/^([^<]+)(?:<([^>]+)>)*$/',
  'main<subkey><nothersub><onemore>',
  $keys
);
var_dump($keys);

Expected result:
----------------
array(
  'main<subkey><nothersub><onemore>',
  'main',
  'subkey',
  'nothersub',
  'onemore'
)

Actual result:
--------------
array(
  'main<subkey><nothersub><onemore>',
  'main',
  'onemore'
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-03 20:15 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2016-10-03 20:15 UTC] bwoebi@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

As per http://www.pcre.org/pcre.txt (the documentation of the underlying library):

       If a capturing subpattern is matched repeatedly, it is the last portion
       of the string that it matched that is returned.

this is expected behavior.
 [2016-10-03 20:22 UTC] kael dot shipman at gmail dot com
Oops, sorry for making extra work, and thanks for the clarification!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC