Well, MRE doesn't say that those regexen are to be used to find integers -- in fact, it states: 'We don't know what these might be used for ...'.

However, if you do want to find (and extract) just integers you could employ both negative look-ahead, and negative look-behind (lookhind wasn't available when MRE was written):

my $numbers="1 1.0 3.547 123 92.34 343.2234"; while($numbers=~/(?<![\d.])(\d+)(?![\d.])/g){ print "$1 is an integer\n"; }

That says: match a bunch of digits that are not preceded by a digit or a dot, and are not followed by a digit or a dot (there be other particular exceptions that I'm not considering at the moment such as grabbing any leading + or - sign if present, but this gives you something to play with).

Update: Ahh, and an important exception I neglected is caught by mirod's version: namely, an integer could occur at the end of a sentence and be legitimately followed by a dot, thus we may want to allow for a trailing dot as long as it isn't followed by digits by changing the negative look-ahead above to the one mirod uses: (?!\d*\.\d).


In reply to Re: How can I match all the integers in a string? by danger
in thread How can I match all the integers in a string? by Anonymous Monk

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.