snacktime has asked for the wisdom of the Perl Monks concerning the following question:
I think there is a better way of doing this, but I couldn't think of it.
I have a string where I need to rewrite each occurence of [string] with the following:
__[username]__[string]__
There can be multiple instances of [string] in a line, and I don't want to match [string] a second time once it's been rewritten.
Here is what I am doing now. I know there has got to be a better way, but this does work.
my $prefix = "__" . $self->{user} . "__"; my $suffix = "__"; my %hash; my $c =0; my @contexts = sort {length $b <=> length $a} @list; foreach my $context (@contexts) { if($var_val =~s/($context?)/\@\@$c\@\@/g) { $hash{$c} = "$prefix$1$suffix"; $c++; } } foreach my $key (keys %hash) { $var_val =~s/\@\@$key\@\@/$hash{$key}/g; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex help
by Roy Johnson (Monsignor) on Jun 02, 2005 at 17:45 UTC | |
by ikegami (Patriarch) on Jun 02, 2005 at 18:18 UTC | |
by snacktime (Sexton) on Jun 02, 2005 at 17:59 UTC | |
Re: Regex help
by ikegami (Patriarch) on Jun 02, 2005 at 17:41 UTC | |
by snacktime (Sexton) on Jun 02, 2005 at 17:54 UTC | |
by ikegami (Patriarch) on Jun 02, 2005 at 18:11 UTC | |
Re: Regex help
by Joost (Canon) on Jun 02, 2005 at 17:44 UTC | |
Re: Regex help
by Jasper (Chaplain) on Jun 02, 2005 at 17:50 UTC |
Back to
Seekers of Perl Wisdom