Forum Moderators: coopster

Message Too Old, No Replies

Regex \w

\w matching lowercase but not upper case

         

PHP_Chimp

2:11 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_match('%^[^,.-_!?\s][\w\s,.!?-]+$%', 'test') is passing
preg_match('%^[^,.-_!?\s][\w\s,.!?-]+$%', 'tesT') is passing
preg_match('%^[^,.-_!?\s][\w\s,.!?-]+$%', 'Test') is not passing

\w should = [a-zA-Z_]

What am i missing?

[edited by: PHP_Chimp at 2:18 pm (utc) on Sep. 27, 2007]

vincevincevince

2:18 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[^,.-_!?\s] << this catches your first character (T or t)

Try testing with 'tEst' and 'test' and you'll see what I mean

PHP_Chimp

2:23 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ye, got it. Thanks.
Although it is now working with '%^[^,.-_!?\s]?[\w\s,.!?-]+$%' I thought that each character class only matched a single character unless you use one of the modifiers. So [^,.-_!?\s] should only match a whitespace character or any of the other punctuation in there, and only for the first character as it was not followed by any modifier.

Sometimes I love regex, today is one of those days when I hate them :(

Achernar

12:15 pm on Sep 28, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Look at the code matching the first character:
[^,.-_!?\s]

and in particular this element:
.-_

This is a sequence. It matches any char whose ascii code is between '.' and '_', which includes :
./0123456789:;<=>?@
uppercase letters: A to Z
[\]^_

This is why your expression can't match a word with a leading letter in uppercase.

Try this instead:
[^,._!?\s-]

[edited by: Achernar at 12:16 pm (utc) on Sep. 28, 2007]