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;
| [reply] [d/l] [select] |
Re: help required for regexp
by BrowserUk (Patriarch) 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".
| [reply] [d/l] [select] |
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;
| [reply] [d/l] |
Re: help required for regexp
by Corion (Patriarch) on Dec 07, 2011 at 09:36 UTC
|
%Hidlabels = {3333=>1, 4444=>2, 2222=>3};
$formula =~ s/f(\d+)/$Hidlabels{$1}/ge;
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: help required for regexp
by AnomalousMonk (Archbishop) 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'
| [reply] [d/l] |
Re: help required for regexp
by sumeetgrover (Monk) 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;
| [reply] [d/l] [select] |
Re: help required for regexp
by tobyink (Canon) 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/;
| [reply] [d/l] |
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";
| [reply] [d/l] |
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;
| [reply] [d/l] |
|
|
Nobody seems to have posted a complete solution that actually works.
Neither did you.
Update: Eliya's solution seems to work.
| [reply] |
|
|
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.
| [reply] |
|
|
|
|
|
|