Re: preg_match too greedy

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

 



On 07/29/2009 03:03 PM, Ben Dunlap wrote:
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?

5.2.9

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.

Really? I think it makes it crystal clear that it's the '/' that is optional. In any case, it makes no difference.


echo (preg_match($pattern, $test) != false)

The " != false " here is redundant.

Understood. But what you think is redundancy is, to me, clarity in programming. I happen to think that boolean tests shouldn't ride on whether or not an array returned from a function is empty or not (or a freaking boolean). If what I'm looking for is a "false" then that's what I'll test for.

Combined with the ternary operator, the logical switchbacks make me a
> little dizzy (especially this close to lunchtime).


Oh, you're one of those people who can't stand the sight of a ternary operator. Buck up, son, it's good for ya ;-)

(Seriously: I don't understand why ternaries freak some people out. It's plain as day!)


--
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