I'm not too shabby at REGEXes but this one isn't behaving as I'd expect.

I want to remove all redundant numbers and their separators in a string (the numbers are always 2 digits) like:

12-12 -> 12
12-12-12 -> 12
12-13-12-13 -> 12-13
12-12-13-13- -> 12-13
12-13-13-14 -> 12-13-14

and so on.

I figured this was a good start:

s/(\d\d)(-\d\d)*?(-\1)/$1$2/g;

but although it DOES remove a number, it (to me, unexpectedly) only removes one. I read that as:
FIND A NUMBER
LOOK FOR other seperator / numbers, miminally
find a seperator and matching number to the first one
replace it with the initial number and the minuimally matched stuff
repeat

I get this:
12-14-12-12 -> 12-14-12

I realize I could put this in some sort of while () loop until it no longer matches but that seems like it should be unnecessary.

I know I need a non-consumiung match on the (-\d\d)*? part so I tried: ?=(-\d\d)*? instead, but then it never matched ANYTHING....

Wise ones please enlighten me? Perhaps there is a switch that tells the regex to go back to ^ after each substitution?

Thank-You.

In reply to How can I reduce this with a REGEX? by misterperl

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.