Re: including . in a pattern match
- From: keithw@xxxxxxxxxxxxxxxxx (Keith Worthington)
- Date: Mon, 30 Jan 2006 10:07:40 -0500
On Fri, 27 Jan 2006 18:31:18 -0500, Chas Owens wrote
> 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"; } }
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
> For additional commands, e-mail: beginners-help@xxxxxxxx
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
Chas,
Wow, it is going to take me some time to wrap my head around this code. I
really like the commenting idea. That certainly will help the next time around.
I don't get the lines where you defined the pattern.
i.e. my $border = qr{ cool_pattern_stuff }xi
Hmm is qr something special? I guess I will start with the x modifier man page.
What I would like to do first is to use this ($RE{num}{real}) construct. That
will simplify the code enormously. I am off to the man pages.
http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common.pm
http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common/number.pm
Ahhh, a bright day dawns with plenty of opportunity for learning in sight. :-)
Kind Regards,
Keith
.
- Follow-Ups:
- 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
- Re: including . in a pattern match
- From: Chas Owens
- including . in a pattern match
- Prev by Date: Re: Create thumbnail from webpage
- 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
|
|