Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: String regex replacement

by moritz (Cardinal)
on Feb 24, 2014 at 10:53 UTC ( #1075967=note: print w/replies, xml ) Need Help??

in reply to String regex replacement

my %replacement = ( 500 => '000', 988 => '000', 1210 => '1000', 1300 => '1000', 2134 => '2000, 2500 => '2000', ); my $regex = join '|', keys %replacement; $str =~ s/\b($regex)\b/$replacment{$1}/;

Untested, but it should work pretty much like this.

Replies are listed 'Best First'.
Re^2: String regex replacement
by Skeeve (Vicar) on Feb 24, 2014 at 12:02 UTC

    Funny, exact, to the point -> ++!

Re^2: String regex replacement
by FloydATC (Deacon) on Feb 26, 2014 at 07:27 UTC all we need is a script to generate that %replacement hash :o)

    -- FloydATC

    Time flies when you don't know what you're doing

      How about this?

      use strict; use warnings; use Data::Dumper; my $post = "Hi, erm does anyone know how to do a string replacement th +at caters for all the following scenario? 500 changes to 000 or 0 988 + changes to 000 or 0 1210 changes to 1000 1300 changes to 1000 2134 c +hanges to 2000 2500 changes to 2000 Thanks in advance."; my %replacement = $post =~ /(\d+) changes to (\d+)/g; print Dumper \%replacement;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1075967]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-09-19 21:51 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (229 votes). Check out past polls.