Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

help required for regexp

by upaksh (Novice)
on Dec 07, 2011 at 09:02 UTC ( #942181=perlquestion: print w/ replies, xml ) Need Help??
upaksh has asked for the wisdom of the Perl Monks concerning the following question:

Hello

,

I have following string

 $str = "f3333_4444_2222";

I want to replace the string and make it

 $str = "f1_2_3"

The regexp i am using is

%Hidlabels = {3333=>1, 4444=>2, 2222=>3}; $formula =~ s/f(\d+)/$labels{ $Hidlabels{$1} }/g;

but this replaces everything with 1. ne ideas?

Comment on help required for regexp
Select or Download Code
Re: help required for regexp
by Eliya (Vicar) on Dec 07, 2011 at 09:15 UTC

    What is %labels?  Also, your declaration of %Hidlabels should use parentheses, not curlies.  As for the regex, you're matching on f(\d+), which only occurs once in the string...

    This works for me:

    my $str = "f3333_4444_2222"; my %Hidlabels = ( 3333=>1, 4444=>2, 2222=>3 ); $str =~ s/(\d+)/$Hidlabels{$1}/g;
Re: help required for regexp
by BrowserUk (Pope) on Dec 07, 2011 at 09:25 UTC

    Take a look at the transliteration operator, tr///:

    $str = "f3333_4444_2222";; $str =~ tr[342][123]s;; print $str;; f1_2_3

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: help required for regexp
by si_lence (Deacon) on Dec 07, 2011 at 09:25 UTC
    If your example is not a simplified case to illustrate your problem you could also use
    $str =~ s/(\d)\d+/$1/g;
Re: help required for regexp
by Corion (Pope) on Dec 07, 2011 at 09:36 UTC

    Maybe you want the /e switch (see perlre)?

    %Hidlabels = {3333=>1, 4444=>2, 2222=>3}; $formula =~ s/f(\d+)/$Hidlabels{$1}/ge;

      Why /e ?  The substitution side behaves like a double quoted string, so $Hidlabels{$1} already interpolates.

        Uh - yes - I'm not thinking clearly.

Re: help required for regexp
by TJPride (Pilgrim) on Dec 07, 2011 at 11:23 UTC
    Nobody seems to have posted a complete solution that actually works.

    use strict; use warnings; my %Hidlabels = ( '3333' => 1, '4444' => 2, '2222' => 3 ); my $str = 'f3333_4444_2222'; $str =~ s/f(\d+)/$Hidlabels{$1}/g; print $str;
      Nobody seems to have posted a complete solution that actually works.
      Neither did you.
      Update: Eliya's solution seems to work.
        Odd, I could have sworn I read all the posts on the way by. If not, then I apologize for posting essentially a duplicate solution. However, mine does in fact work if you run it, so you're wrong on that.
Re: help required for regexp
by sumeetgrover (Scribe) on Dec 07, 2011 at 15:55 UTC

    The first part of your regular expression would only match:
    f3333
    And therefore replace 3333 with 1. Also, you haven't mentioned the %labels hash but used it in regexp.

    Your regexp can be made more specific and changed to:
     $formula=~s/([0-9]{4})/$Hidlabels{$1}/g;

Re: help required for regexp
by pvaldes (Chaplain) on Dec 07, 2011 at 19:01 UTC

    I want to replace the string and make it

    Simply assign a new value to the variable, regexp are slow, don't use it if you can cheat:

     $str = "f1_2_3";
Re: help required for regexp
by AnomalousMonk (Abbot) on Dec 07, 2011 at 22:32 UTC

    I may be reading too much into the OP, but if upaksh wants to translate digit groups in strings like 'f3333_4444_2222' only when the prefix character is an 'f', the other solutions fail (as shown below). Here are a couple of approaches:

    >perl -wMstrict -le "my %Hidlabels = ('3333' => '1', '4444' => '2', '2222' => '3'); my $t = 'f3333_4444_2222 g4444_2222_3333 f4444_2222_3333 f3333 _3333'; ;; my $s = $t; $s =~ s{ (\d+) }{$Hidlabels{$1}}xmsg; print qq{'$s'}; ;; $s = $t; $s =~ s{ (?: \G (?<! \A) _ | f) \K (\d+) }{$Hidlabels{$1}}xmsg; print qq{'$s'}; ;; $s = $t; my $f = qr{ f \d{4} (?: _ \d{4}){2} }xms; $s =~ s{ ($f) } { (my $x = $1) =~ s{(\d+)}{$Hidlabels{$1}}xmsg; $x; }xmsge; print qq{'$s'}; " 'f1_2_3 g2_3_1 f2_3_1 f1 _1' 'f1_2_3 g4444_2222_3333 f2_3_1 f1 _3333' 'f1_2_3 g4444_2222_3333 f2_3_1 f3333 _3333'
Re: help required for regexp
by tobyink (Abbot) on Dec 08, 2011 at 12:41 UTC
    %Hidlabels = (3333=>1, 4444=>2, 2222=>3); $formula =~ s{ (\d+) } { defined $Hidlabels{$1} ? $Hidlabels{$1} : $1 }xeg if $formula =~ /^f/;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-09-18 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls