Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Match/Replace/Interpolate

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:

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

Comment on Match/Replace/Interpolate
Download Code
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

        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.

        /J\

        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.

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

    --shmem

    _($_=" "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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://562803]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2015-07-06 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls