paulbooker has asked for the wisdom of the Perl Monks concerning the following question:
As an exercise I am reducing the size of an svg file with a number of successful substitutions.
But removing trailing zeroes from decimal fractions is beating me.
s/(-?\d\.\d*)/$1 + 0/ge is giving the best result for me, but is also removing decimal points from urls in line 1 of the svg. So zeroes are nicely removed but the svg is broken in both cases.
Can anyone spot why? (Strawberry Perl on Win 7)
wdsmin.plsample.svg#!/usr/bin/perl use v5.22; use strict; use warnings; open my $fh, '<', 'sample.svg' or die "Cannot read : $!\n"; open my $out_fh, '>', 'samplemin.svg' or die "Cannot write : $!\n"; while (<$fh>) { s/(-?\d\.\d*)/$1 + 0/ge; # trailing zeroes on decimal fractions print $out_fh $_; } close $out_fh; <>;
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org +/1999/xlink" version="1.1" width="148.00mm" height="105.00mm" viewBox +="0 0 56.1468 39.8339"> <line transform="translate(0.0000, 6.6544)" stroke-width="0.2000" stro +ke="#000" x1="20.0390" y1="-0.0000" x2="56.1077" y2="-0.0000"/> </svg>
s/(-?\d\.\d*)/$1 + 0/ge removes "." between w3 and org <svg xmlns="http://www.w3org/2000/svg" xmlns:xlink="http://www.w3org/1999/xlink"
s/(-?\d*\.\d*)/$1 + 0/ge also replaces "." with 0 between www and w3 <svg xmlns="http://www0w3org/2000/svg" xmlns:xlink="http://www0w3org/1999/xlink" plus a "use warnings" flag about "." being not numeric.
I could repair the svg header with another line of Perl, but I just want to understand what's going on here.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex for trailing zeroes
by Eily (Monsignor) on Feb 23, 2016 at 12:49 UTC | |
by paulbooker (Initiate) on Feb 23, 2016 at 12:58 UTC | |
|
Re: regex for trailing zeroes
by choroba (Cardinal) on Feb 23, 2016 at 12:49 UTC | |
by RonW (Parson) on Feb 27, 2016 at 00:03 UTC |