http://www.perlmonks.org?node_id=562803

toadi has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

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.


--
My opinions may have changed,
but not the fact that I am right