good chemistry is complicated,
and a little bit messy -LW
How can I reduce this with a REGEX?by misterperl (Beadle)
|on Mar 14, 2014 at 15:30 UTC||Need Help??|
misterperl has asked for the
wisdom of the Perl Monks concerning the following question:
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:
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
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?