Re: preg_match too greedy
| From: | b | Date: | Thu, 30 Jul 2009 02:16:34 +0000 |
| Subject: | Re: preg_match too greedy | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296044@lists.php.net to get a copy of this message | ||
On 07/29/2009 03:03 PM, Ben Dunlap wrote:
Jim Lucas wrote:5.2.9[8<]I expected 'no match' but get 'match'.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:Really? I think it makes it crystal clear that it's the '/' that is optional. In any case, it makes no difference.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.$url = '/foo(/)?';
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.The " != false " here is redundant.echo (preg_match($pattern, $test) != false)
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!)