php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55654 ereg() behavior for preg_match
Submitted: 2011-09-09 12:30 UTC Modified: 2015-05-28 13:12 UTC
From: imaggens at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Regexps related
PHP Version: 5.3SVN-2011-09-09 (snap) OS: Windows 7
Private report: No CVE-ID: None
 [2011-09-09 12:30 UTC] imaggens at gmail dot com
Description:
------------
Consideration. I choosen "September Snapshot", because I could not find mine in 
the 
list. My installation report to "PHP 5.3.3. Build Date: Jul 21 2010 20:25:38".

Alright.

I would like to ask, if is there any possibility to add, maybe through another 
non-Perl compatible modifier, the behavior we had with ereg().

The behavior I'm talking about refers to match as much as possible instead of 
stop at very first valid match.

This is useful sometimes. In my case, specially to validate input data against a 
RFC specification.

Look at this snippet: https://ideone.com/sC6mA

I tried to make it as much specific as I could.

The intention was to validate float point numbers, between zero and 1, with none 
and up to three decimals, denying invalid floats, such as 0.00 (same as zero) or 
1.0 (same as 1).

But, the "lazy" behavior of preg_match() is accepting the code above, where 
0.3444 should be denied, because of its 4 decimals.

But since 0.344 is valid in the last length verification (one and up to three), 
the function accepts the input data, and the last digit is simply ignored, 
because preg_match() already caracterized 0.344 as valid.

I hope you understand

Expected result:
----------------
An empty array

Actual result:
--------------
A match

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-15 15:37 UTC] nikic@php.net
I don't know what your exact use case is, but

... if you want to check that a string is a float, you should surround the regex  with ^ and $ anchors. I.e. it will match the complete string, not just parts of it.
... if you are searching for floats in a longer text, you could simply use a negative lookahead assertion (?![0-9]) to ensure it isn't followed by a number.

If neither are what you need, could you maybe explain your problem further?
 [2011-09-27 21:00 UTC] imaggens at gmail dot com
Hi,

Sorry for the long time to reply. I was unable to keep this task for a while.

Where exactly you think should I add this lookahead assertion? As far as I know, it should be added one line after the last comment (the one which make all the floating part optional).

But I have no success.

As you said I can't match against start / end anchors because the float number may appear in several parts of input string.
 [2015-05-28 13:12 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-05-28 13:12 UTC] cmb@php.net
Actually, your regex might match up to four decimal digits
(\.[1-9][1-9]{1,3}). A probably suitable regex can be seen on
<http://3v4l.org/GZ9MZ>.

Anyhow, I'm closing this request, because I consider it highly
unlikely and unnecessary that a simplified regex syntax will be
made available for the PCRE implementation.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Oct 19 12:01:27 2019 UTC