Re: including . in a pattern match
- From: chas.owens@xxxxxxxxx (Chas Owens)
- Date: Fri, 27 Jan 2006 18:31:18 -0500
Another thing you can do is break your larger regexes into parts to
make them more readable/maintainable. It also helps to use the x flag
so that you can separate the individual tokens and comment them.
#!/usr/bin/perl
use strict;
use warnings;
use Regexp::Common;
#FIXME: the border value should be (foo|bar|baz) where foo,
#bar, and baz are the valid values for a border
my $border = qr{ # match a border value
border: #constant indicating this is a border value
\s* #optional spaces
(.*) #the border value
}xi;
my $num = qr{ #match a number of feet or inches
($RE{num}{real}) #a real number
(['"]|'') #unit indicator ' is foot, " or '' is inches
}x;
my $dim = qr{ #match a dimension
$num #height value
\s* #optional spaces
(?:x|by) #x or by, don't capture
\s* #optional spaces
$num #width value
}xi;
my $size = qr{ #match a size value
size: #constant indication this is a size value
\s* #optional spaces
$dim #the dimensions
}xi;
my $tag = qr{ #match a tag value
tag: #constant indication this is a tag value
\s* #optional spaces
(.*) #the tag
}xi;
my $match=qr{ #match the whole record for a widget
^ #start of the record
$border #a border value
\s* #optional spaces
$size #a size value
\s* #optional spaces
$tag #a tag value
$ #optional spaces
}x;
while(<>) {
chomp;
if (/$border\s*$size\s*$tag/) {
my ($border, $h, $h_type, $w, $w_type, $tag) =
($1, $2, $3, $4, $5, $6);
"Border is $border\n",
"Height is ", ($h_type !~ /"|''/ ? $h*12 : $h), " inches\n",
"Width is ", ($w_type !~ /"|''/ ? $w*12 : $w), " inches\n",
"Tag is $tag\n";
} else {
print "invalid entry\n";
}
}
.
- Follow-Ups:
- Re: including . in a pattern match
- From: Keith Worthington
- Re: including . in a pattern match
- From: Chas Owens
- Re: including . in a pattern match
- References:
- including . in a pattern match
- From: Keith Worthington
- Re: including . in a pattern match
- From: Chas Owens
- including . in a pattern match
- Prev by Date: Re: including . in a pattern match
- Next by Date: Re: including . in a pattern match
- Previous by thread: Re: including . in a pattern match
- Next by thread: Re: including . in a pattern match
- Index(es):
Relevant Pages
|