Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Match/Replace/Interpolate

by toadi (Chaplain)
on Jul 21, 2006 at 08:34 UTC ( [id://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

Replies are listed 'Best First'.
Re: Match/Replace/Interpolate
by Corion (Patriarch) 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.

        /J\

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\/$_>/

    --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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found