Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: In-place regex substitution

by toolic (Bishop)
on Dec 09, 2016 at 21:39 UTC ( #1177571=note: print w/replies, xml ) Need Help??


in reply to In-place regex substitution

If you want to change the string in place, you can use s///ge:
use warnings; use strict; my $content = '{ "geometry": { "type": "Polygon", "coordinates": [ [ [ + 19.054804912278406, 47.485785556135411 ], [ 19.057857836771483, 47.4 +87322542030711 ], [ 19.06025597925397, 47.488491565765209 ], [ 19.060 +347248086835, 47.488539642204628 ], [ 19.060463310421543, 47.48457828 +7406251 ], [ 19.054804912278406, 47.485785556135411 ] ] ] } }'; $content =~ s/(\d\d\.\d*)/substr($1,0,9)/ge; print "$content\n"; __END__ { "geometry": { "type": "Polygon", "coordinates": [ [ [ 19.054804, 47. +485785 ], [ 19.057857, 47.487322 ], [ 19.060255, 47.488491 ], [ 19.06 +0347, 47.488539 ], [ 19.060463, 47.484578 ], [ 19.054804, 47.485785 ] + ] ] } }

Replies are listed 'Best First'.
Re^2: In-place regex substitution
by AnomalousMonk (Bishop) on Dec 10, 2016 at 00:06 UTC

    Or without explicit capturing or evaluation (or rounding!) (update: and with dynamic control of truncation threshold), but needs Perl version 5.10+ for  \K operator:

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $content = qq/{ \"geometry\": { \"type\": \"Polygon\", \"coordinates\": [ [ +[ 19.05480 4912278406, \n/ . qq/47.485785556135411 ], [ 19.057857836771483, 47.487322542030711 + ], [ 19.06025597925397, \n/ . qq/47.488491565765209 ], [ 19.060347248086835, 47.488539642204628 + ], [ 19.060463310421543, \n/ . qq/47.484578287406251 ], [ 19.054804912278406, 47.485785556135411 + ] ] ] } } \n/ ; print qq{[[$content]]}; ;; my $n = 7; $content =~ s{ \d [.] \d{$n} \K \d+ }{}xmsg; ;; print qq{<<$content>>}; " [[{ "geometry": { "type": "Polygon", "coordinates": [ [ [ 19.054804912 +278406, 47.485785556135411 ], [ 19.057857836771483, 47.487322542030711 ], [ 19 +.06025597925397, 47.488491565765209 ], [ 19.060347248086835, 47.488539642204628 ], [ 19 +.060463310421543, 47.484578287406251 ], [ 19.054804912278406, 47.485785556135411 ] ] ] } + } ]] <<{ "geometry": { "type": "Polygon", "coordinates": [ [ [ 19.0548049, 47.4857855 ], [ 19.0578578, 47.4873225 ], [ 19.0602559, 47.4884915 ], [ 19.0603472, 47.4885396 ], [ 19.0604633, 47.4845782 ], [ 19.0548049, 47.4857855 ] ] ] } } >>


    Give a man a fish:  <%-{-{-{-<

Re^2: In-place regex substitution
by lmocsi (Novice) on Dec 09, 2016 at 21:45 UTC
    Great, thx! :)
      Are you sure that a number can't have more than 9 digits before the point?

      You might prefer sprintf instead of substr in order to just shorten the digits after the point.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2022-01-24 19:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?