Bug #78191 [Opn->Nab]: /^[a-z]+$/ can be match "\n"
| From: | danack@php.net | Date: | Fri, 21 Jun 2019 02:01:45 +0000 |
| Subject: | Bug #78191 [Opn->Nab]: /^[a-z]+$/ can be match "\n" | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-221425@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78191&edit=1
ID: 78191
Updated by: danack@php.net
Reported by: sunweiqiang8 at gmail dot com
Summary: /^[a-z]+$/ can be match "\n"
-Status: Open
+Status: Not a bug
Type: Bug
Package: *Regular Expressions
Operating System: CentOS 7.6
PHP Version: 7.2.19
Block user comment: N
Private report: N
New Comment:
This is the same behaviour as Perl, and comes from the PCRE library used by PHP.
https://www.regular-expressions.info/anchors.html
"Because Perl returns a string with a newline at the end when reading a line from a file,
Perl's regex engine matches $ at the position before the line break at the end of the string
even when multi-line mode is turned off. Perl also matches $ at the very end of the string,
regardless of whether that character is a line break. So ^\d+$ matches 123 whether the subject
string is 123 or 123\n."
If you don't want this, you can use the D modifier.
https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
"D (PCRE_DOLLAR_ENDONLY)
If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the
subject string. Without this modifier, a dollar also matches immediately before the final character
if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is
set. There is no equivalent to this modifier in Perl."
Previous Comments:
------------------------------------------------------------------------
[2019-06-21 01:28:05] sunweiqiang8 at gmail dot com
Description:
------------
Only PHP can succeed.
# php
preg_match("/^[a-z]+$/", "abc\n");
#int(1)
# javascript
var reg = /^[a-z]+$/;
console.log(reg.test("abc\n"));
# false
# java(jdk11)
System.out.println(Pattern.matches("^[a-z]+$", "abc\n"));
# false
Test script:
---------------
<?php
var_dump(preg_match("/^[a-z]+$/", "abc\n"));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78191&edit=1