Consider how   m!(\d+).+to.+(\d+)!i matches against   123to456 Since you're using greedy matching, each + will match as much as it can. The regexp matches like this:
(12)3to45(6) $1 $2
That's not what you intend. A first cut at fixing this is to rewrite the regex so that it it won't gobble up extra digits on either side of "to".   m!(\d+)\D+to\D+(\d+)!i; This will match target strings that have non-digit substrings surrounding the "to", but won't match "123to456", since there are no non-digit characters surrounding "to". If that's a problem, you can take the regex a step further, and write   m!(\d+)\D*?to\D*?(\d+)!i; which will accept zero or more non-digit characters on either side of "to".


In reply to Re: $1 and regex by dws
in thread $1 and regex 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.