in reply to check if string contains anything other than alphanumeric
From your description I think you need the \w character class,
If you want to exclude the underscore as well then you have to say the character class explicitly,$ perl -le 'print "hello world" =~ /^\w+$/ ? "OK" : "BAD"' BAD ---> contain spaces $ perl -le 'print "helloworld" =~ /^\w+$/ ? "OK" : "BAD"' OK $ perl -le 'print "hello_world" =~ /^\w+$/ ? "OK" : "BAD"' OK ---> underscore is considered alphanumeric character.
Since version 5.6.0, Perl supports [:class:] style of character class, and the equivalet for above is [:alnum:].$ perl -le 'print "hello_world" =~ /^[a-zA-Z0-9]+$/ ? "OK" : "BAD"' BAD ---> underscore is not in the class
HTH,$ perl -le 'print "hello world" =~ /^[[:alnum:]]+$/ ? "OK" : "BAD"' BAD ---> space is not alnum $ perl -le 'print "hello_world" =~ /^[[:alnum:]]+$/ ? "OK" : "BAD"' BAD ---> _ is not alnum $ perl -le 'print "HelloWorld1" =~ /^[[:alnum:]]+$/ ? "OK" : "BAD"' OK --> nothing there but letters and digits
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: check if string contains anything other than alphanumeric
by jwkrahn (Abbot) on Aug 12, 2007 at 12:39 UTC | |
by naikonta (Curate) on Aug 12, 2007 at 14:08 UTC |