In addition to previous mentions, you also need () around whatever you want to capture in the $1 variable.
if ($temp2 =~ m/^([a-zA-Z]+)$/) {$naam = $1;}
-jbWare
| [reply] [d/l] |
if ($temp2 =~ m/^([a-zA-Z]+)$/) { $naam = $1; }
else { die "Wrong input"; }
What you wrote matches only strings that contain the substring 'a-zA-Z' in them. You need to enclose ranges like 'A-Z' within [ ] for them to be interpreted as character classes. Also, by anchoring the regexp (with ^ and $1) you ensure that the entire string must match. The last thing is that you need that quantifier + so that one or more characters will match; without it, the only strings that will match are those consisting of a single letter.
1Some people prefer to use \z instead of $ for the end-of-string anchor, because $ doesn't unconditionally match the end of the string; indeed, it ignores one optional trailing newline. I think of $ as basically shorthand for /\n?\z/... Well, it's more complicated than that, because the meaning of $ changes in the presence of the /m modifier, but not so for /\n?\z/... A lot of ins, a lot of outs, a lot of what-have-yous, a lot of strands to keep in the ol' duder's head...
Update: In my original reply, I focused on the why the regexp was not matching, and failed to notice that you were using $1 later on. I fixed the regexp to reflect this. Sorry for the confusion.
| [reply] [d/l] [select] |
Alternately, you can make sure that it doesn't contain characters that aren't the class.
if ( $temp2 =~ /[^a-zA-Z]/ ) {
die "Wrong input";
}
else {
# Do something useful
}
This is a very strong way to work. You're whitelisting and not blacklisting, which is best.
| [reply] [d/l] |
Thanks for the information
| [reply] |
You're only checking if there's at least one character matching your character class anywhere in your input. Anchor your regex: $temp2 =~ m/\A[a-zA-Z]+\z/. Read perldoc perleretut and perldoc perlre.
And remember to use <code></code> tags next time.
| [reply] [d/l] [select] |
$temp2 =~ /^[a-zA-Z]+$/
# ^ denotes the beginning
# + denotes one or more matches
# $ denotes the end
You may want to see perldoc perlre.
| [reply] [d/l] |
You already have some good answers, but here is another that just reverses your search (ie instead of searching for a-zA-Z it searches for anything that is not a-zA-Z and negates the answer):
$temp2 !~ m/[^a-zA-Z]/
This will return false if it finds a character that is not a-z or A-Z. The [^ ] only matches if none of the characters contained in the brackets are found.
You could reverse your condition to make the code easier to read with this method:
if ($temp2 =~ m/[^a-zA-Z]/)
{
die "Wrong input";
}
else
{
$naam = $1;
}
- Cees | [reply] [d/l] [select] |
lots of good answers--just want to tack on one note since OP said he's a regex first-timer..
These are all equivalent -- the /i on the end makes it case insensitive.. check the perlre man page for other modifiers (/m, /s, /x, and /g for substitutions).
m/[a-zA-Z]/
m/[a-z]/i
m/[A-Z]/i
| [reply] [d/l] |
#!/usr/bin/perl
use strict;
use warnings;
use YAPE::Regex::Explain;
$\ = "\n*************\n";
# Your original
print YAPE::Regex::Explain->new(qr/[a-zA-Z]/)->explain;
print YAPE::Regex::Explain->new(qr/[a-zA-Z]+/)->explain;
print YAPE::Regex::Explain->new(qr/([a-zA-Z]+)/)->explain;
# The solution
print YAPE::Regex::Explain->new(qr/^([a-zA-Z]+)$/)->explain;
Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")
Don't fool yourself.
| [reply] [d/l] |