Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change


by toadi (Chaplain)
on Jul 21, 2006 at 08:34 UTC ( #562803=perlquestion: print w/replies, xml ) Need Help??
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.

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

Replies are listed 'Best First'.
Re: Match/Replace/Interpolate
by Corion (Pope) on Jul 21, 2006 at 08:38 UTC

    I'd do the whole search-and-replace thing in one step instead of two steps:

    $line =~ s!<gid=(\d+)>!<img src="PATH/TO/$1">!g;

    I think the only problem with your approach is that it lacked the /g "global" modifier that replaces all occurrences within one line.

    20060721 shmem spotted a missing > sign, thanks!

      Hi Corion, thanks for the reply. What do the ! mean instead of the normal / in the regex?

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

        The ! are used as alternative delimiters, because you use / within the regular expression. This makes it easy to avoid the escaping of slashes. Instead of \/, I now can write / as it will not be recognized as the regex delimiter anymore.

        perlop has the section on Regexp Quote-like Operators which explains how this works.

        They don't mean anything special, you can replace the '/' with any other character (or matching pairs of brackets) to improve readability and reduce the requirement for escaping of literal '/' characters in your expression.


Re: Match/Replace/Interpolate
by shmem (Chancellor) on Jul 21, 2006 at 09:35 UTC
    Just a typo ($1 and $_). You mean:
    $line =~ s/<gid=$_>/<img src="PATH\/TO\/$_>/


    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://562803]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-06-18 00:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.