The approach of factoring regex sub-expressions can also be helpful. $RE{num}{real} is from Regexp::Common. The $title regex won't properly match something like '/foo/bar/test.c' so this regex (and the others) may need to be refined; this is easier to do if regexes have been factored into individual components.
c:\@Work\Perl\monks>perl -wMstrict -le
"use Regexp::Common;
;;
my @lines = (
'test1.cpp 0.00% of 21 0.00% of 16',
'test2.c None 16.53% of 484',
'test3.h 0.00% of 138 None',
'/x/y/foo.c 0.00% of 1 None',
);
;;
my $title = qr{ \w+ (?: [.] \w+)* }xms;
my $percent = qr{ $RE{num}{real} % \s+ of \s+ \d+ }xms;
my $none = qr{ None }xms;
;;
for my $line (@lines) {
print qq{line '$line'};
die qq{bad line: '$line'} unless
my ($t, $p1, $p2) = $line =~ m{
\A ($title) \s+ ($percent | $none) \s+ ($percent | $none) \s* \
+z
}xms;
print qq{ title: '$t' pcent1: '$p1' pcent2: '$p2'};
}
"
line 'test1.cpp 0.00% of 21 0.00% of 16'
title: 'test1.cpp' pcent1: '0.00% of 21' pcent2: '0.00% of 16'
line 'test2.c None 16.53% of 484'
title: 'test2.c' pcent1: 'None' pcent2: '16.53% of 484'
line 'test3.h 0.00% of 138 None'
title: 'test3.h' pcent1: '0.00% of 138' pcent2: 'None'
line '/x/y/foo.c 0.00% of 1 None'
bad line: '/x/y/foo.c 0.00% of 1 None' at -e line 1.
Update: Changed code example to better demonstrate error handling.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|