For example data, for example:
my $temp_comment = 'CR1234: "Then some text" CRs 2345, 3456, 45, 567 CR678 "Some Text"';
to try:
$temp_comment =~ s/(,)|[^\d\n]+/$1?' ':''/semg;
or if you want to stay close to string patterns:
$temp_comment =~ s/ ^ # multi-line mode, line start \s* # leading blanks? CR # CR tag \D* # non-number stuff ( # start capture group (?:\d+ [,\s]*)+ # find (number, comma, space) groups ) # end capture group \D* # skip remaining non-number stuff $ # multi-line mode, line end /$1/mxg;
but you will need to remove the commas in the group of numbers in the next step.
$temp_comment =~ tr/,//d;
or
$temp_comment =~ s/(?<=\d),(?=\s\d)//g;
For a "single step" you should use the /e modifier:
$temp_comment =~ s{ ^ # line start \s* # leading blanks? CR # CR tag \D* # non-number stuff ((?:\d+ [,\s]*)+) # single or group of numbers \D* # non number stuff $ # line end } {do{(local$_=$1)=~y/,
This, according to the above data, will lead to:
1234 2345 3456 45 567 678
But really, use , if possible, a simpler two-step approach . The last regex can be a nightmare for your successors.