Page 2 of 2

Posted: Sat Nov 19, 2005 11:37 am
by sweatje
d11wtq wrote: That's a lookbehind done wrong :P

I guess you meant a lookahead ;)

\d(?!0)

o_O d11wtq is Mr Pedantic for the rest of the day :P
Mr Pedantic, looks like you need to brush up on your regex a bit ;) Here is a simpletest test case.

Code: Select all

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'));
 
}
 

Posted: Sat Nov 19, 2005 2:33 pm
by Chris Corbyn
Ah I see what you meant lol.... the tests made it clearer :)

I though you meant any number of regex less than ten (i.e. 1-9) ... I hadn't considered you were looking for a number not starting with a zero.

*shy's away* 8O

Posted: Thu Aug 31, 2006 9:27 pm
by feyd
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.

Here's the link supplied by "the manual:" http://www.amazon.com/exec/obidos/ASIN/ ... /wwwphpnet

Posted: Thu Aug 31, 2006 10:35 pm
by Luke
that's funny... I just put that on my wish list today. About an hour ago.

Re: Welcome to the new regex forum.

Posted: Thu Jun 20, 2013 4:58 am
by ducdm87
i see http://www.regular-expressions.info/tutorial.html is good tutorial
What sites do you know good online regex test. I see http://php.toolregex.com use good, easy to use interface