toadi has asked for the wisdom of the Perl Monks concerning the following question:
I am a bit stuck here in finding a way how to do this. I'm trying to match some value in some text to get a number <gid=1234>. I want to replace this tag with <img src="PATH/TO/$id"> where the $id will be the number I matched eg. 1234.
Matching and fetching the id is no problem, but trying to replace the correct entry gives me a hassle. Especially when there are more entries in one line.
Some example code:
use strict; while (<DATA>) { my $line = $_; print $. . " " . $line; my @matches = /<gid=(\d+)>/g; foreach (@matches) { print "MATCH:: $_\n"; $line =~ s/<gid=$1>/<img src="PATH\/TO\/$1>/ } print "NEW" . $line; } __DATA__ <gid=3035>Wij zijn hier aan het moven. We gaan nog meer moven. <gid=30 +36> Waarom wij dit doen weet ik ook niet. Maar we zullen het snel weten. < +gid=3037> <gid=3037>we testen dit zo.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Match/Replace/Interpolate
by Corion (Patriarch) on Jul 21, 2006 at 08:38 UTC | |
by toadi (Chaplain) on Jul 21, 2006 at 08:58 UTC | |
by Corion (Patriarch) on Jul 21, 2006 at 09:01 UTC | |
by gellyfish (Monsignor) on Jul 21, 2006 at 09:01 UTC | |
Re: Match/Replace/Interpolate
by shmem (Chancellor) on Jul 21, 2006 at 09:35 UTC |