function testRegexJokeExpression() {
// how many regex does it take to change a lightbulb
// correct answer, any non-zero digit
$regex = '/\d(?<!0)/';
$this->assertFalse(preg_match($regex,'0'));
$this->assertTrue(preg_match($regex,'1'));
$this->assertTrue(preg_match($regex,'10'));
//another suggested regex
$regex = '/\d(?!0)/';
// this test fails with your regex
$this->assertFalse(preg_match($regex,'0'), 'fails to fail for case 0');
$this->assertTrue(preg_match($regex,'1'));
$this->assertTrue(preg_match($regex,'10'));
//modified this way, it actually would work
$regex = '/(?!0)\d/';
$this->assertFalse(preg_match($regex,'0'));
$this->assertTrue(preg_match($regex,'1'));
$this->assertTrue(preg_match($regex,'10'));
}
Last edited by sweatje on Sat Nov 19, 2005 2:36 pm, edited 1 time in total.
This is a good a place as any to drop a resource. I don't know if anyone noticed, but O'Reilly released a new edition of Mastering Regular Expressions. This version includes a bunch of pages on PHP apparently.