|  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: not-implemented at mark-plomer dot de
New email:
PHP Version: OS:


 [2020-06-03 11:51 UTC] not-implemented at mark-plomer dot de
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:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-03 12:05 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-06-03 12:05 UTC]
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: <>.

[1] <>
 [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-2022 The PHP Group
All rights reserved.
Last updated: Wed Jun 29 08:05:46 2022 UTC