in reply to check if string contains anything other than alphanumeric

Yes, perlre is the right direction :-) You can use character class where you can group some characters based on certain specification, and in this case you can make arbtrary spec. Perl provides some symbols for predefined character class, such as \w for alphabet and numeric and underscore ("_"), \d for digits only, and \s for all kind of recognized spaces.

From your description I think you need the \w character class,

$ 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.
If you want to exclude the underscore as well then you have to say the character class explicitly,
$ perl -le 'print "hello_world" =~ /^[a-zA-Z0-9]+$/ ? "OK" : "BAD"' BAD ---> underscore is not in the class
Since version 5.6.0, Perl supports [:class:] style of character class, and the equivalet for above is [:alnum:].
$ 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
HTH,

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
    $ perl -le 'print "hello_world" =~ /^\w+$/ ? "OK" : "BAD"' OK ---> underscore is considered alphanumeric character.
    Underscore is not considered an alphanumeric character. It is considered a word character (hence the \w character class.)
      It's said that \w matches a 'word' character (alphanumeric plus '_'), so I should have said that "underscore is considered part of that character class". Thank you, jwkrahn.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!