Re: including . in a pattern match



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



Relevant Pages

  • 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)
  • Re: WebBrowser border
    ... DOCHOSTUIFLAG_NO3DBORDER flag from GetHostInfo. ... this works out on WinCE. ... Instead of simply returning E_NOTIMPL I now fill the DOCHOSTUIINFO ... the term "3D Border" is confusing me. ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Re: How do I join 2 tables
    ... "Jay Freedman" wrote in message ... >>paragraph mark between the 2 tables. ... >>paragraph that separates the two tables, it keeps it as two separate ... > separate border? ...
    (microsoft.public.word.tables)
  • Re: Borders and styles
    ... > transparent border around cells that have a background color... ... > border-collapse:separate may work but it doesn't seem to separate far ...
    (microsoft.public.frontpage.client)