Re: including . in a pattern match



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);
print
"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";
}
}
.



Relevant Pages

  • do not display text border
    ... I do not want to display its border. ... how to set the flag? ... Prev by Date: ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: unicycle games
    ... Something I've always wanted to try was unicycle capture the flag. ... Either choose to make players "out" ... Attempt to sneak across the border, find and capture the enemy flag, ...
    (rec.sport.unicycling)
  • Re: including . in a pattern match
    ... It also helps to use the x flag ... > so that you can separate the individual tokens and comment them. ... and baz are the valid values for a border ...
    (perl.beginners)
  • WxRuby widget borders
    ... I can add a border around widgets in WxRuby using the ALL flag, ... the LEFT, RIGHT, TOP and BOTTOM flags aren't recognized. ...
    (comp.lang.ruby)
  • Re: Minutemen arent Considered Citizens
    ... I'd say let the immigrants in and end the "border patrol". ... display the American flag, and show who's side you are on. ... don't have any patriotism... ...
    (alt.politics.bush)