Re: preg_match too greedy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



b wrote:
> On 07/29/2009 07:48 PM, Daniel Kolbo wrote:
>>
>> code works (no match) for me too on php 5.2.6 build date May 2 2008
>> 18:01:20 with dumbdows NT.
>>
>> preg_match fails but for a reason other than what I think you may be
>> expecting.  It fails b/c of the first forwards slash in $url.  The regex
>> engine doesn't even get past the second character let alone reaching the
>> end of line anchor.
> 
> The forward slash shouldn't be an issue as the delimiter is '%'. The
> full pattern is:
> 
> %^/foo/?$%U
> 
> 
>> Also, your code works (no match) with this first forward slash removed:
>> $url = 'foo(/)?'
> 
> But the string happens to start with a forward slash.
> 
> 
i am not talking about the delimiter
your pattern is:
%^/foo/?$%U
your test string is:
'foo/bar'

the first character of your pattern is a "/" and your first character of
your test string is an "f".  They do not match.  The regex engine stops
after checking the first character.  This has nothing to do with
greediness or end of line anchors, but only with the first character
comparisons.

maybe what you wanted for your test string was
'/foo/bar'

This test string would then require your end of line anchor. Because the
end of line character does not match the "b" the engine stops.  No match.

This is consistent with the findings of others who replied to you.

Perhaps your regex engine has a different syntax for anchors.  For
example if your engine was seeing the carrot as a an exclusion operator
rather than being a beginning of line anchor and it was also, idk,
ignoring the dollar sign as your end of line anchor then you would get a
match.  But otherwise I would recommend you copy/paste the example you
provided and confirm that you still get a match.  Then i would confirm
your regex engine's anchor syntax.  Then, recompile your software....
dK
`

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux