Regular expression equivalent to !=

I puzzled this morning for quite some time over something that would be trivial in PHP, but needed to happen in a regular expression. I was using a form validator, and it uses a regular expression to parse a form input. But the field I was trying to check has a default value of “Please choose…”. So a simple test for a null value would not work.

What to do?

Negative Look-behinds! The following will return true if the input does not match ‘Please choose…’:

$result = preg_match('/^.+$(?<!Please choose...)/',$country);

So the match works because it is selecting any line in its entirety, but then rejecting any line that does not include the match string.

Beats re-writing the validator function, but you have to admit, this would have been a whole lot cleaner and easier to read:

$result = $country != 'Please choose...';

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options