Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

I have been searching all over the internet for examples of how this is done, I just must be typing the wrong keywords in. But basically Ive seen examples like node 6059

One Example that is given is this:
if ($string =~ m!(^[a-z0-9]{4})$!) { $string =~ tr/[a-z]/[A-Z]/; # do something } else { # do some error }

What does all that "m!(^[a-z0-9]{4})$!)" mean! I understand it wants characters a-z,0-9 only 4 characters.

Basically, all I need to do is 'validate' three variables and remove any characters besides 0-9 a-z. So very simple, but since I dont under that that '!!<_:"!@#!@" stuff, begin semi-newish still to perl, i'm totally lost.

Replies are listed 'Best First'.
Re: String Validation
by chakram88 (Pilgrim) on Mar 07, 2007 at 19:19 UTC
    Ok, you're probably getting lost with some of the delimiters and the anchors that are in there.

    The ! are used to delimit the regex, instead of the standard /. This is often done to make the regex easier to read when certain meta characters need to be escaped in the regex.

    In this particular case, they could have just as easily been the standard / making the regex look like this:

    m/(^[a-z0-9]{4})$/

    Does that look a bit more familiar to you?

    The ^ and $ are there to anchor the regex. And there's a set of parenthesis to capture/cluster.

    That should be enough to help you.

    Some kind monk (sorry, I forget who) recently pointed out a nice CPAN module YAPE::Regex::Explain, that is quite useful:

    #!/usr/bin/perl use warnings; use strict; use YAPE::Regex::Explain; my $re = qq[m!(^[a-z0-9]{4})$!]; my $yape = YAPE::Regex::Explain->new($re); print $yape->explain; __END__ The regular expression: (?-imsx:m!(^[a-z0-9]{4})) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- m! 'm!' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- [a-z0-9]{4} any character of: 'a' to 'z', '0' to '9' (4 times) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Re: String Validation
by shandor (Monk) on Mar 07, 2007 at 19:10 UTC

    I have been searching all over the internet for examples of how this is done, I just must be typing the wrong keywords in.

    Check out perlre. This will explain a bunch of regular expression characters that can be confusing.

    Or, if you're really interested, go find a copy of O'Reilly's great Regular Expressions book.

    UPDATE: added quote so that this doesn't look like an RTFM post. :)

Re: String Validation
by Trihedralguy (Pilgrim) on Mar 07, 2007 at 19:28 UTC
    Those last two posts really helped, thanks a bunch.
    I really just couldnt find a good example of this anywhere, I dont know why, so that is why I asked here.
    Thanks again for the amazing assistance, this website is really helping me learn perl :)
Re: String Validation
by ysth (Canon) on Mar 07, 2007 at 19:10 UTC
    Can you say what kind of validation you want to do? What's valid and what's not?
Re: String Validation
by davorg (Chancellor) on Mar 08, 2007 at 08:47 UTC
Re: String Validation
by Trihedralguy (Pilgrim) on Mar 07, 2007 at 19:13 UTC
    The validation I need is

    if $string has anything other than A-Z or 0-9 - PASS
    else FAIL.
      if ($string =~ /[^A-Z0-9]/) { # String contains a character other than A-Z, 0-9. ... } else { # String only contains characters A-Z, 0-9. ... }

      or

      if ($string =~ /^[A-Z0-9]*\z/) { # String only contains characters A-Z, 0-9. ... } else { # String contains a character other than A-Z, 0-9. ... }
      #!/usr/bin/perl -l use strict; use warnings; my $regexp = qr/^[^A-Z0-9]+$/; print 'abc' =~ $regexp; print 'ABC' =~ $regexp; print 'ab0' =~ $regexp; print '^|+' =~ $regexp; print '' =~ $regexp;
      prints
      1 (0) (0) 1 (0)
Re: String Validation
by Trihedralguy (Pilgrim) on Mar 09, 2007 at 19:41 UTC
    There is ONE thing I still down understand and thats reguarding !@#$%^&* symbols. The field I'm checking is 'lastname' I want to make sure they cant put any bad things like % in there, but I need to allow ' or " " (space) so that people like O'Mally or Las Luther can still "pass". I havent found one example that shows how to do this.
      if ($lastname =~ /[^a-zA-Z' -]/) { die("Bad character\n"); }
      • [...] matches any one character among those listed or those in the listed character ranges.
      • [^...] matches any one character not among those listed or those in the listed character ranges.

      Ref: perlre