Is there a quick way to check if a scalar contains non-alpha-numeric characters?

Discussion in 'Perl Misc' started by John Smith, Sep 26, 2003.

  1. John Smith

    John Smith Guest

    Hi all,

    I want to ensure that a variable does not contain spaces, and other
    non-alpha-numeric characters such as !#$%^&*()-=+, etc... Basically, this
    variable is a username that someone is sending to me and will only be 4 to
    10 characters long. It must only contain numbers and letters, otherwise I
    will reject it.

    Is there a quick way to do this or must I create a loop and check every
    letter?

    Thanks for all,

    Guy Doucet
     
    John Smith, Sep 26, 2003
    #1
    1. Advertisements

  2. John Smith

    Anno Siegel Guest

    No. A regular expression (or a tr///) and a call to length() should do.

    What have you tried so far?

    We are happy to help with code, but not happy to write solutions from
    scratch.

    Anno
     
    Anno Siegel, Sep 26, 2003
    #2
    1. Advertisements

  3. That is easily done with a regular expression.

    perldoc perlre

    http://www.perldoc.com/perl5.8.0/pod/perlre.html
     
    Gunnar Hjalmarsson, Sep 26, 2003
    #3
  4. reject($username) unless $username =~ /^[A-Za-z\d]{4,10}$/;
     
    Glenn Jackman, Sep 26, 2003
    #4
  5. I think you mean /^[A-Za-z0-9]{4,10}$/ or maybe /^[[:alnum:]]{4,10}$/

    Putting \d inside a character class matches '\' and 'd', not digits.

    I'll let someone else worry about locale, although I suspect it doesn't
    matter much for the OP.
     
    David K. Wall, Sep 26, 2003
    #5
  6. Not true. The pseudo-classes \w, \W, \s, \S, \d, and \D work fine in a
    character class.
     
    Jeff 'japhy' Pinyan, Sep 26, 2003
    #6
  7. John Smith

    Uri Guttman Guest

    DKW> Putting \d inside a character class matches '\' and 'd', not digits.

    perl -le 'print "match" if "123" =~ /[\d]/'
    match

    you can put any char class shortcut (\d,\s,\D, etc.) into a char
    class. it just is merged (union in set terms) with all the other chars
    and shortcuts in that char class.

    uri
     
    Uri Guttman, Sep 26, 2003
    #7
  8. Ah, the use of the [:alnum:] class would be the best solution, as it is
    probably resistant to changes in locale or use of unicode.
     
    Glenn Jackman, Sep 26, 2003
    #8
  9. Sheesh, my batting average is sucking. And I even tested it. (Note to
    self: SLOW DOWN)
     
    David K. Wall, Sep 26, 2003
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.