If you only want to match a bunch of digits followed by .txt, then
/^\d+\.txt$/
should do the trick. The ^ and $ "anchor" the regex to the beginning and end of the string, so that matches the beginning of the string, followed by one or more digits, followed by '.txt', followed by the end of the string. If you define 1.1.txt as being OK (and the way I read it, 1.1 is a number), then:
/^\d+(\.\d+)?\.txt$/
will work. (...)? matches zero or one instances of whatever's inside the brackets, making the decimal point and any digits after it optional. And finally, to optionally allow negative numbers:
/^-?\d+(\.\d+)?\.txt$/
The brackets in those last two, which I'm really just using for grouping, will have the side-effect of capturing their contents into the $1 variable. You can avoid that by inserting more line-noise into the pattern, which I left out here in the interests of clarity. Search for the word 'clustering' in the perlre manpage for details.

A completely different approach would be to use perl's automatic conversion between strings and numbers. If you evaluate a scalar that begins with a number in a numberish way - eg by adding it to 0 - the result is the number at the beginning of the string. For instance:

print 0+"1.1.1.txt" # 1.1 print 0+"-3.14.foo" # -3.14
or if you only want integers ...
print int("1.1.1.txt") # 1 print int("-3.14.foo") # -3
Adapting your code to use this method - and to round your numbers properly - is left as an exercise for the reader :-)

In reply to Re: regex matching number by DrHyde
in thread regex matching number by InfiniteSilence

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.