Re: Regular Expression help

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

 



Arpad Ray wrote:
> Note that $ allows a trailing newline, but \z doesn't.

I had to test that before believing you:

php -r 'var_dump(preg_match("#^[a-z]+\$#","abc"),preg_match("#^[a-z]+\$#","abc\n"),preg_match("#^[a-z]+\z#","abc\n"));'

you are right, that could consitute a nice big gotcha in some situations,
although I have the habit of running trim on everything bit of data that
has to be validated/santized (prior to any more specific checks/cleansing)
so I have been protecting my self unwittingly:

php -r 'var_dump("abc\n", trim("abc\n"));'

none the less I think I'll be using \z more often now that I have
been properly introduced to it (including answering any regexp questions
around here!)

thanks Arpad :-)


> 
> Arpad
> 
> Stut wrote:
>> Chris Boget wrote:
>>> <?php
>>> echo 'Is String: [' . ( is_string( 'a1b2c3' ) && preg_match(
>>> '/[A-Za-z]+/', 'a1b2c3' )) . ']<br>';
>>> echo 'Is Numeric: [' . ( is_numeric( 'a1b2c3' ) && preg_match(
>>> '/[0-9]+/', 'a1b2c3' )) . ']<br>';
>>> echo 'Is String: [' . ( is_string( 'abcdef' ) && preg_match(
>>> '/[A-Za-z]+/', 'abcdef' )) . ']<br>';
>>> echo 'Is Numeric: [' . ( is_numeric( '123456' ) && preg_match(
>>> '/[0-9]+/', '123456' )) . ']<br>';
>>> ?>
>>>
>>> Why is the first "Is String" check returning true/showing 1? 
>>> preg_match should fail because 'a1b2c3' contains numbers and, as
>>> such, doesn't match the pattern...
>>
>> It does match the pattern. The expression says "1 or more A-Za-z in
>> sequence". If you want to check against the whole string you need to
>> add the start and end markers...
>>
>> preg_match( '/^[A-Za-z]+$/', 'a1b2c3' ))
>>
>> Look at the manual page for preg_match and read up on the third
>> parameter. Use it to see what is matching the expression.
>>
>> -Stut
>>
> 

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