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!


In reply to Re: check if string contains anything other than alphanumeric by naikonta
in thread check if string contains anything other than alphanumeric by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.