php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79670 Regex with non-greedy dot-all does not match "$" correctly
Submitted: 2020-06-03 11:51 UTC Modified: 2020-06-03 12:05 UTC
From: not-implemented at mark-plomer dot de Assigned: cmb (profile)
Status: Not a bug Package: PCRE related
PHP Version: 7.3.18 OS: any
Private report: No CVE-ID: None
 [2020-06-03 11:51 UTC] not-implemented at mark-plomer dot de
Description:
------------
The following regex with non-greedy dot-all selector does not consume the last newline from input. Note that the PCRE-Modifier "m" is NOT used, so "$" should match the end-of-string only.

Test script:
---------------
$x = preg_replace('/A.*?$/s', 'X', "A\n");
echo json_encode($x);

Expected result:
----------------
"X"

Actual result:
--------------
"X\n"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-03 12:05 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-06-03 12:05 UTC] cmb@php.net
From the PHP manual[1]:

| […] while the "end of line" metacharacter ($) matches only at
| the end of the string, or before a terminating newline (unless D
| modifier is set).

So you may want to use the D modifier: <https://3v4l.org/ea3SQ>.

[1] <https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php>
 [2020-06-03 12:37 UTC] not-implemented at mark-plomer dot de
Oh wow ... even after coding PHP for so many years, there is always something new to learn :D

Thanks for reading the docs for me ;-)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC