Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

correct usage of q, quotemeta, with s///

by cidaris (Friar)
on May 30, 2002 at 22:16 UTC ( [id://170532]=perlquestion: print w/replies, xml ) Need Help??

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

I have a giant chunk of HTML, tags, quotes and all. I have done this:
my $page = ## the contents of the entire page in a string ##; my $old_text = qq ( LOTSOFHTML ); my $new_code = qq ( LOTSTOREPLACEWITH );


I cannot get  $page =~ s/$old_text/$new_code/g to work for the life of me. Should I use quotemeta instead, or simply 'q' instead of 'qq'.

Thanks
Aaron

Replies are listed 'Best First'.
Re: correct usage of q, quotemeta, with s///
by I0 (Priest) on May 31, 2002 at 00:58 UTC
    $page =~ s/\Q$old_text\E/$new_code/g;
Re: correct usage of q, quotemeta, with s///
by Joost (Canon) on May 31, 2002 at 09:36 UTC
    You must use some kind of quoting before you can use arbitrary strings in a regex, otherwise the special characters are interpreted as regex constructs instead of regular substrings.

    As noted above, you can use either the \Q...\E construct or quotemeta() - it works something like this:

    my $string = 'abc!hu('; print "\Q$string\E\n"; # prints abc\!hu\( print quotemeta $string; # prints abc\!hu\(
    The \Q .. \E construct is a little easier in this case, because you can embed it in your match - you should not use it in the replacement part of a substitution, because that's (a lot like) a regular double-quoted string:
    s/\Q$string\E/$replacement/; # or .. $qstring = quotemeta $string; s/$qstring/$replacement/;

    For (a lot more) info, take a look at

    perldoc perlre perldoc perlop (the m// part)
    </code>
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: correct usage of q, quotemeta, with s///
by termix (Beadle) on May 30, 2002 at 22:36 UTC

    You have a single space before and a single space after both the strings. Is that a required effect? Notice:

    my $page="nospaceLOTSnospace space LOTS space"; my $old_text = qq ( LOTS ); my $new_text = qq ( LESS ); $page =~ s/$old_text/$new_text/g; print $page;

    ... will output ...

    nospaceLOTSnospace space LESS space

    The first "LOTS" does not get replaced because you are replacing " LOTS " (space before and after) with " LESS "

    -- termix

      The spacing is irrelevant, (perhaps I should have noted that) I simply used it for illustration purposes.

      "LOTS" simply indicates several lines of HTML that include double-quotes, slashes, you name it. (Note: several lines)

      my $page is also several lines of HTML, the contents of an entire file, actually.
      I'm trying to replace multiple lines of code (compressed into a single string) in one big string (the entire file) with another string (more lines of code).

      I hope this is somewhat clearer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found