Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I keep anything other than alphanumeric out of a variable?

( #8384=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Apr 21, 2000 at 19:18 UTC
Q&A  > data formatting


Description:

I tried both of these, but neither one worked:

$user_name =~ s/[a-zA-Z0-9]//g; $user_name !~ s/[a-zA-Z0-9]//g;

Answer: How do I keep anything other than alphanumeric out of a variable?
contributed by DrHyde

The following gets rid of non-alphanumerics and underscores:

$user_name =~ s/[\W_]//g;

The pattern [\W_] breaks down as follows:

  • [...] any of the characters from the character class consisting of ...
  • \W any "non-word" character ...
  • _ or an underscore

However, instead of just silently cleaning data, I'd prefer to check the string for undesirable characters and notify the user if it is bad, so that they can fix it:

$user_name =~ /[\W_]/ and warn "user name is bad\n"

Answer: How do I keep anything other than alphanumeric out of a variable?
contributed by turnstep

As long as you are willing to concede that an underscore '_' is alphanumeric, you can use this:

$user_name =~ s/\W//g;
\w is shorthand for the character class [A-Za-z0-9_] and \W is the inverse of that set, i.e. [^\w].

Answer: How do I keep anything other than alphanumeric out of a variable?
contributed by btrott

Your regex says, "find the alphanumeric characters in $user_name, and replace them with nothing."

You want the opposite:

$user_name =~ s/[^a-zA-Z0-9]//g;
The ^ at the beginning of the character class inverts the set, i.e. "all things not in this character class".

Answer: How do I keep anything other than alphanumeric out of a variable?
contributed by Roy Johnson

The right tool for character classes is tr///, not s///:

$user_name =~ tr/0-9a-zA-Z//dc;
(You can add the underscore character, or any others you like, of course.)

If you wanted the username to look like a valid Perl identifier (i.e., begin with a letter, then alphanumerics + underscores), you would then want to strip off the leading non-letters:

$user_name =~ s/^[^a-z]*//i;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (8)
    As of 2014-12-27 16:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (177 votes), past polls