use strict; use warnings; my @a = map { s/\s*(#.*)?$//g; $_ } ; my @b; while (local $_ = shift @a) { if (m/\D(\d+)-(\d+)(?:\D|$)/) { my $range = "$1-$2"; foreach my $i ($1..$2) { local $_ = $_; s/(\D)$range(\D|$)/$1$i$2/; push @a, $_; } } else { push @b, $_; } } print join("\n", @b); __END__ 172.17.119.2 # Comments are here... 172.17.119.4-5 # Comments are here... 172.19-21.254.2-3 # Comments are here... 192.168.1.1-3 # Comments are here...