my $string = "rue' version='4' changeset='6456424' lat='36.7256674' lon='-4.4539391' />"; sub modify_lat_lon { my $lat = shift; # lat='1.11' my $lon = shift; # lon='2.22' my $fill = shift; # whatever is between the stuff above $lat =~ s/-?[\d.,]+/new_lat/; $lon =~ s/-?[\d.,]+/new_lon/; # recombine the parts to one string return $lat . $fill . $lon; } # one way (lat .. lon) or the other (lon .. lat) $string =~ s/(lat='[^']+')(.*)(lon='[^']+')/modify_lat_lon($1,$3,$2)/e || $string =~ s/(lon='[^']+')(.*)(lat='[^']+')/modify_lat_lon($3,$1,$2)/e; print $string, $/;