#!/usr/bin/perl -l
# http://perlmonks.org/?node_id=1168288
use strict;
use warnings;
my @ra = qw(0001 0002 0003 011 012 013 015 16 17 18 20);
$_ = join ',', @ra;
s/\b # find a number boundary
(\d+) # and a number $1
(?{$1}) # save the number in $^R
\K # then leave the above in the string
(?: # non-capture grouping
, # comma - also forces $1 to be a complete number
(\d+) # a complete number (because of , and \b)
\b # forces comlete number
(??{ # extended pattern
++$^R != $2 # false if correct number
||
length$1 != length$2 # false if same length
}) # false (and therefor matches) if both false, other
+wise fails
# if true returns 1 which can't match because of th
+e \b
)+ # repeat at least once
/-$2/gx; # replace second or more with last matching number
print;
|