Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

String regex replacement

by Anonymous Monk
on Feb 24, 2014 at 08:52 UTC ( #1075953=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, erm does anyone know how to do a string replacement that 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 changes to 2000 2500 changes to 2000 Thanks in advance.

Comment on String regex replacement
Re: String regex replacement
by Corion (Pope) on Feb 24, 2014 at 09:04 UTC

    How would you determine whether 500 changes to 000 or 0 ? When should it change to 0?

Re: String regex replacement
by hdb (Parson) on Feb 24, 2014 at 09:15 UTC

    What you describe, sounds like rounding down to the next 1000s. Have a look at this example:

    use strict; use warnings; my @numbers = ( 500, 988, 1210, 1300, 2134, 2500 ); # rounding numerically for (@numbers) { my $n = int( $_/1000 )*1000; print "$_ => $n\n"; } # rounding in regex for (@numbers) { s{(\d+)}{int( $1/1000 )*1000}e; print "$_\n"; }
Re: String regex replacement
by Ratazong (Prior) on Feb 24, 2014 at 09:17 UTC

    I assume you just want to "nullify" the last 3 digits of your numbers. You could do it with the following code:

    my @strings=("500", "988", "1210", "1300", "2000"); foreach my $in (@strings) { print "$in "; $in =~ s/(\d\d\d)$/000/; # take the last 3 digits, replace them w +ith 000 print "-> $in\n"; }
    hth! Rata

Re: String regex replacement
by kcott (Abbot) on Feb 24, 2014 at 09:30 UTC

    Without punctuation and string delimiters, it's very difficult to know exactly what you're asking. The guidelines in "How do I post a question effectively?" will help you post a better question.

    Regex substitution is performed with s///. You should probably read "perlrequick - Perl regular expressions quick start" and "perlretut - Perl regular expressions tutorial" to get a basic understanding of Perl's regular expressions before doing anything else.

    It sounds like you probably want a lookup table and a regex using alternation. Here's a very simplistic example of the technique:

    #!/usr/bin/env perl -l use strict; use warnings; my %lookup = ('500' => '000', '0 988' => '000', '0 1210' => '1000'); my $re = '(' . join('|' => keys %lookup) . ')'; my $string = 'www "999" xxx "0 1210" yyy "500" zzz "0 988"'; print $string; $string =~ s/$re/$lookup{$1}/g; print $string;

    Output:

    www "999" xxx "0 1210" yyy "500" zzz "0 988" www "999" xxx "1000" yyy "000" zzz "000"

    Depending on the context of the strings to be replaced, the pattern may need to be a lot more complex than that shown. For instance, given '500 988', do you want to replace 500' and ignore ' 988' or ignore '50' and replace '0 988'. Reading the documentation I linked to will help you understand these issues and how to deal with them. And, of course, assuming '0 988' is a string to be replaced is just a guess on my part (as I alluded to in the opening paragraph).

    -- Ken

Re: String regex replacement
by moritz (Cardinal) on Feb 24, 2014 at 10:53 UTC

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


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

      ...now 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075953]
Approved by hdb
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2014-08-20 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (124 votes), past polls