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.pl
#!/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; <>;
sample.svg
<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.


In reply to regex for trailing zeroes by paulbooker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.