While the module suggested initially is really cool and useful, the alternative, without using a special module, would be:
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";
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".

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.


In reply to Re: A regex question with 2 pieces of data by graff
in thread A regex question with 2 pieces of data by bilbozilla

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.