in reply to A regex question with 2 pieces of data
Here, the "qr" operator is used to save a regex pattern in the scalar "$decimal" (see the perlop man page for "qr"); then, that regex variable is used twice on the target string, and the match is done in a list context (assigning to two scalars), so the two parenthesized matches are assigned to "$frst" and "$scnd".my $string = <<EOT; <span class="style">28.56</span><span class="style">-1.22</span><span +class="style">-4.1</span><span class="style">04/02</span> EOT my $decimal = qr/[-+]?(?:\d+\.?\d*|\d*\.\d+)/; my ( $frst, $scnd ) = ( $string =~ /($decimal).*?($decimal)/ ); print "$frst, $scnd\n";
As for the $decimal regex itself, it's looking for a pattern where there may or may not be an initial hyphen or plus sign, then either one or more digits (with optional period and zero or more digits) or else a period with one or more digits (see the perlre man page regarding the "(?:...)" syntax and related tricks).
Note that this will not handle variants like "2.4e7", and other possible "rare" forms -- although it would certainly be possible to add the necessary conditions. But that is why we like to use modules for this sort of thing, because the module will normally cover all that without requiring us to make our own coding more complicated.
|
|---|