in reply to Replace part of a regex match
If you're really committed to the regexp, this works:
my $u = 'http://adserver.adtech.de/?addyn|2.0|323|91793|1|277|target=_ +blank'; print "$u\n"; $u =~ s/(.*\|.*\|.*\|.*\|.*\|).*(\|.*)/${1}976$2/; print "$u\n"; __END__ http://adserver.adtech.de/?addyn|2.0|323|91793|1|277|target=_blank http://adserver.adtech.de/?addyn|2.0|323|91793|1|976|target=_blank
You might also try using split and join:
my $u = 'http://adserver.adtech.de/?addyn|2.0|323|91793|1|277|target=_ +blank'; print "$u\n"; my @fields = split /\|/, $u; $fields[5] = 976; $u = join '|', @fields; print "$u\n"; __END__ http://adserver.adtech.de/?addyn|2.0|323|91793|1|277|target=_blank http://adserver.adtech.de/?addyn|2.0|323|91793|1|976|target=_blank
I also think it would be good to have a more specific expression. For example, instead of ".*", use "\d+" to make sure it's not an empty field and to make sure that it really does have digits and not some other junk you weren't expecting.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Replace part of a regex match
by Bloodnok (Vicar) on Dec 23, 2008 at 23:04 UTC | |
by AnomalousMonk (Archbishop) on Dec 24, 2008 at 06:43 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |