Re: Regex confusion...
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Fri, 28 Sep 2007 00:10:42 +0100
Quoth guthrie <guthrie@xxxxxxx>:
sorry for the beginner question, but...
With this code
my $img = "0-12345-abc";
print " Match.1 ", (defined $img);
print " Match.2 ", ($img =~ /\S/);
print " Matched::", $1;
You should never use the $N variables without checking the match
succeeded. In any case, your pattern has no capturing parens, so $1 will
be empty.
Others have already noted that \S and \w only match single characters.
The actual code I'm trying for is:
if(defined $img and $img =~ /\S/) {
if ($img =~ /^(\d)-(\d+)-(\w)$/)
{ my ($t, $zip, $type) = ($1, $2, $3); }
This can be simplified to
if (
my ($t, $zip, $type) =
$img =~ /^(\d)-(\d+)-(\w+)$/
) {
which avoids the need to use the $N variables altogether.
Ben
.
- Follow-Ups:
- Re: Regex confusion...
- From: sln
- Re: Regex confusion...
- References:
- Regex confusion...
- From: guthrie
- Regex confusion...
- Prev by Date: Re: tail recursion
- Next by Date: FAQ 4.58 How can I know how many entries are in a hash?
- Previous by thread: Re: Regex confusion...
- Next by thread: Re: Regex confusion...
- Index(es):