Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Quickie Question: search and replace problem

by JojoLinkyBob (Scribe)
on Apr 19, 2001 at 00:13 UTC ( [id://73641]=perlquestion: print w/replies, xml ) Need Help??

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

I'll try to get straight to the problem:
$attempt1 = "get_stuff()"; $attempt2 = "get_stuff\(\)"; $attempt3 = qq(get_stuff()); $newstr = "apple"; $data = $attempt1; $data =~ s/$attempt1/$newstr/; print "\n$data"; $data = $attempt2; $data =~ s/$attempt2/$newstr/; print "\n$data"; $data = $attempt3; $data =~ s/$attempt3/$newstr/; print "\n$data";
Output:
apple()
apple()
apple()

Problem: I'm trying to search and replace the entire contents of the $attempt strings above, but it always stops at the (). Is there any elegant way to do this? I've looked all over my little back book of perl :(


Thanks,
Desert coder

Replies are listed 'Best First'.
Re: Quickie Question: search and replace problem
by indigo (Scribe) on Apr 19, 2001 at 00:19 UTC
    Basically, $data is intepreted as a string, and $attempt1 as a regex, so if you have special regex characters, they will never match up, no matter how you escape them.

    To make a regex treat $attempt1 as a string, use \Q and \E:
    $data =~ s/\Q$attempt1\E/$newstr/
    man perlre for more info.

    Updated: Added rant, as per tye :)

      I've created a monster! ;)

      Actually, you can eventually quote them enough to get the same effect as \Q and \E. I personally like the /\Q$string\E/ construct and recommend it over the other methods I'm about to mention.

      But you can also do any of these:

      $attempt= "get_stuff\\(\\)"; $attempt= "\Qget_stuff()\E"; $attempt= quotemeta( "get_stuff()" ); $attempt= qr/get_stuff\(\)/; $attempt= 'get_stuff\(\)';
      but I still find the original suggestion makes the most sense to me. In a regex (that means only the first half of s///), $x means interpret the contents of $x as a regex while \Q$x\E means interpret the contents of $x as a string.

      Update: Thanks, indigo, I missed that!

              - tye (but my friends call me "Tye")
        All these do the escaping properly. But the code went like this:
        $data = $attempt; $data =~ s/$attempt/$newstr/;
        The problem isn't simply bad escaping. Either $attempt gets munged, and the match fails, or you have to premunge $data to get $attempt right, and the match still fails.

        So /\Q$attempt\E/ really is the best way to go.
      Thanks! Desert coder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found