Log in

View Full Version : Which Perl regex character matches punctuation characters etc



chriswattsuk
02-12-2009, 09:40 PM
Hi all,

I have the following:


if(!preg_match("/^[a-zA-Z0-9\s]+$/",$value))

Which accepts Hello4 as valid input

But I need it to allow any punctuation too i.e. !?/\<>.,'"@()[]

Is there a character/modifier for this?

Thanks

Chris.:D

jackbenimble4
02-16-2009, 04:40 AM
Hey Chris,

As far as I know, there are no shorthand sequences for punctuation, but you can definitely still achieve that same effect through creating a character class.

For example, if you wanted the code you posted to include those punctuation characters you listed too, you'd change the pattern to the following:


/^[a-zA-Z0-9\s!?/\\<>.,'"@()[\]\-]+$/

Note that only the -, \, ] and ^ characters need to be escaped within a character class.

I wonder what you're trying to prohibit or search for with this pattern? It might be easier to create a negative class and list any characters you don't want rather than all the characters you do.