Req #55654 [Com]: ereg() behavior for preg_match
| From: | nikic@php.net | Date: | Thu, 15 Sep 2011 15:37:25 +0000 |
| Subject: | Req #55654 [Com]: ereg() behavior for preg_match | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163571@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55654&edit=1
ID: 55654
Comment by: nikic@php.net
Reported by: imaggens at gmail dot com
Summary: ereg() behavior for preg_match
Status: Open
Type: Feature/Change Request
Package: Regexps related
Operating System: Windows 7
PHP Version: 5.3SVN-2011-09-09 (snap)
Block user comment: N
Private report: N
New Comment:
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?
Previous Comments:
------------------------------------------------------------------------
[2011-09-09 12:30:43] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55654&edit=1