Dan Fish wrote:
Simple question for the regEXperts out there...
I have a string that is always in the format: aaaaa.nn+x.y
a is always 5 chars
[a-zA-Z0-9]{5}
n can be 1 or 2 digits
[0-9]{1,2}
x can be +/- (with sign), 1-4 digits
[-+][0-9]{1,4}
y is always positive (no sign), 1-4 digits
[0-9]{1,4}
Some examples:
A123C.11+002.001
FC32G.2-1.0
12B15.01+2145.15
I need all 4 pieces and the length of x & y. The following works:
my $id= "A123C.11+002.001";
my @tmp = split(/[\.+-]/,$id);
my $part = $tmp[0];
my $unit = $tmp[1];
my $x = $tmp[2];
my $y = $tmp[3];
my $xlen = length $tmp[2];
my $ylen = length $tmp[3];
but in my quest for understanding regex, I'm always looking for more elegant
(obfuscated :-) code...
Anybody have a good one-liner for this?
my ($part,$unit,$x,$y,$xlen,$ylen) = /* Some obfuscated code here.. */
my ( $part, $unit, $x, $y, $xlen, $ylen ) = (
$id =~ m{
\A
( [a-zA-Z0-9]{5} )
\.
( [0-9]{1,2} )
[-+]
( [0-9]{1,4} )
\.
( [0-9]{1,4} )
\z
}x,
length $3,
length $4,
);
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/