|  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
22 - 5 = ?
Subscribe to this entry?

 [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: Mon Jun 27 06:03:37 2022 UTC