Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

regex (?{CODE}) question

by ctgIT (Initiate)
on Aug 01, 2002 at 19:00 UTC ( #186889=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I have been trying to call Perl code from inside the replace part of a regular expression using the (?{CODE}) construct. My script is as follows:
$str = "(<ref links=\" cit314-1 cit314-2 cit314-3 cit314-4 cit314-5\"/ +>)"; if( $str =~ s/<ref links=\"(.*?)\"(.*?)\/>/<ref links=\"$1\"$2>(?{ pri +nt $1 })<\/ref>/ ){ print "Match was found." . "\n"; } print $str . "\n";

The output I get is:
Match was found. (<ref links=" cit314-1 cit314-2 cit314-3 cit314-4 cit314-5">(?{ print + cit314-1 cit314-2 cit314-3 cit314-4 cit314-5 })</ref>)

Notice that the "print" command was not executed while the $1 was replaced with its value. I would appreciate all help/ideas.
Thanks,
Christos

Replies are listed 'Best First'.
Re: regex (?{CODE}) question
by elusion (Curate) on Aug 01, 2002 at 19:19 UTC
    The print command isn't executed because it's not in the regex. The first portion of the s/// (between the first two //'s) is a regex; the other portion could be considered a string. So you need to place the (?{ print $1 }) at the end of the regex part, like so:
    $str = "(<ref links=\" cit314-1 cit314-2 cit314-3 cit314-4 cit314-5\"/ +>)"; if( $str =~ s/<ref links="(.*?)"(.*?)\/>(?{ print $1, "\n" })/<ref lin +ks="$1"$2><\/ref>/ ){ print "Match was found." . "\n"; } print $str . "\n";
    Notice I also didn't escape the quotes -- this isn't necessary and, I think, it makes things look a little messier.

    elusion : http://matt.diephouse.com

      And don't forget, since you're referring to a capture while still in the regex, it hasn't set $1 yet, so you have to use \1 instead:

       (?{ print $1, "\n" })

      would need to be

       (?{ print \1, "\n" })

      unless there's something special about the codeblock I'm not aware of?

      Update: Strike that, reverse it, elusion's correct. Make sure you don't do anything silly like using \1 instead of $1 ;-)

        While in a code block, $1 is correct. This is necessary because it's executed as actual code. I'm not sure how \1 would actually parse, though I assume it'd be a reference to 1.

        elusion : http://matt.diephouse.com

Re: regex (?{CODE}) question
by rsteinke (Scribe) on Aug 01, 2002 at 19:15 UTC

    Why are you using a print command? It seems like all you're trying to do is insert the value of $1.

    Ron Steinke rsteinke@w-link.net

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2019-08-23 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?