P
perrog
Hi!
I'm new to perl/regular expressions but experience programmer. I'm
trying to match a formatted number 123,456,789 and convert it into an
integer. Thought the following would do, but it don't.
$_ = "1,234,567,890";
my @parts;
(@parts = /(\d{1,3})(?:,(\d{3}))*/g) && do {
my $number = 0;
$number = $number * 1000 + $_ foreach (@parts);
print "$number\n";
};
The problem is that this expression doesn't save repeated groups, it
discard captured repeated groups except the last repeat (so it works
for "123,456" numbers.) If I instead match the below it works, but I
can't understand the logic behind it (if there is any logic?)
(@parts = /(\d{1,3})(?:,(\d{3})+)/g) && do { # not really equivalent
to the above, but almost
my $number = 0;
$number = $number * 1000 + $_ foreach (@parts);
print "$number\n";
};
My second question is, if I can capture repeated groups, how do I know
how many repeats there were. Is there any built-in/special variable
other than $1, $2, etc. @+, @- or the returned array that I'm not
aware of?
Or can't I do it with RE's? Is this an duty for RecDescent? Life would
be more compact with regular expressions.
my $number_parser = Parse::RecDescent->new(q(
parse: digits
digits: /\d{1,3}/ <skip:''> digits_part(s?)
{
my $number = $item[1];
$number = $number * 1000 + $_ foreach (@{$item[3]});
$number;
}
digits_part: "," <skip:''> /\d\d\d/
);
$number_parser->parse("1,234,567,890"); # returns 1234567890
I've searched around, including text books, but could not find any
details how to capture repeated groups (if it now is possible.)
Thanks for any hints.
Regards,
Roggan
I'm new to perl/regular expressions but experience programmer. I'm
trying to match a formatted number 123,456,789 and convert it into an
integer. Thought the following would do, but it don't.
$_ = "1,234,567,890";
my @parts;
(@parts = /(\d{1,3})(?:,(\d{3}))*/g) && do {
my $number = 0;
$number = $number * 1000 + $_ foreach (@parts);
print "$number\n";
};
The problem is that this expression doesn't save repeated groups, it
discard captured repeated groups except the last repeat (so it works
for "123,456" numbers.) If I instead match the below it works, but I
can't understand the logic behind it (if there is any logic?)
(@parts = /(\d{1,3})(?:,(\d{3})+)/g) && do { # not really equivalent
to the above, but almost
my $number = 0;
$number = $number * 1000 + $_ foreach (@parts);
print "$number\n";
};
My second question is, if I can capture repeated groups, how do I know
how many repeats there were. Is there any built-in/special variable
other than $1, $2, etc. @+, @- or the returned array that I'm not
aware of?
Or can't I do it with RE's? Is this an duty for RecDescent? Life would
be more compact with regular expressions.
my $number_parser = Parse::RecDescent->new(q(
parse: digits
digits: /\d{1,3}/ <skip:''> digits_part(s?)
{
my $number = $item[1];
$number = $number * 1000 + $_ foreach (@{$item[3]});
$number;
}
digits_part: "," <skip:''> /\d\d\d/
);
$number_parser->parse("1,234,567,890"); # returns 1234567890
I've searched around, including text books, but could not find any
details how to capture repeated groups (if it now is possible.)
Thanks for any hints.
Regards,
Roggan