Re: preg_match too greedy

From: Date: Wed, 29 Jul 2009 19:03:10 +0000
Subject: Re: preg_match too greedy
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-296017@lists.php.net to get a copy of this message
Jim Lucas wrote: >> I expected 'no match' but get 'match'. [8<] > cut/paste your code and it works for me. Works for me as well. I get 'no match' from PHP 5.1.2, 5.2.6, and 5.2.8. What version do you have? If I might suggest a couple of simplifications that would make it easier to follow/troubleshoot: >> $url = '/foo(/)?'; I don't think you need parentheses around your second forward-slash. If you had multiple characters that were optional you'd want to group them in parentheses, but here I think it just makes the regex harder to read. >> echo (preg_match($pattern, $test) != false) The " != false " here is redundant. Combined with the ternary operator, the logical switchbacks make me a little dizzy (especially this close to lunchtime). Ben

« previous php.general (#296017) next »