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

Howdy,

I'm trying to unescape text that may mean something special when this text is in a variable that's being used in a regular expression, here's an example:

my $foo = "[hello]"; my $bar = "agabaga[hello]zoomzoomzoom"; my $buzz = "goodbye"; $bar =~ s/$foo/$buzz/gi; print "\n$bar\n";

this will faithfully print out:

agabagagoodbyegoodbyegoodbyegoodbyegoodbyezgoodbyegoodbyemzgoodbyegoodbyemzgoodbyegoodbyem

Which isn't exact.y what I wanted, I just wanted hello to be replaced with goodbye. I'm using lil psuedo tags like this for a small templating system, I really like brackets like this, and it seems that there should be an easy way to escape the stuff that the variable holds.

Does anyone know how?

btw, has anyone realized the search on this site is useless? You receive about 100 results that don't help ya out much.

 

-justin simoni
!skazat!

Replies are listed 'Best First'.
Re: Regular Expressions and Variables
by japhy (Canon) on Feb 13, 2001 at 03:34 UTC
    You want to use the \Q...\E escape around the variable in the LHS of your regex (or use quotemeta() on it) to stop Perl from treating it like a regex.
    $from = '[hello]'; $to = '{goodbye}'; $string =~ s/\Q$from\E/$to/; # or $safe_from = quotemeta $from; $string =~ s/$safe_from/$to/;


    japhy -- Perl and Regex Hacker
Re: Regular Expressions and Variables
by MeowChow (Vicar) on Feb 13, 2001 at 04:41 UTC
    Am I the only one who finds it a source of hackish irony and amusement that the brackets [] in skazat's node get interpolated yet again, only this time by PerlMonks rather than the Perl regex engine, resulting in their disappearance and the unintended underlining of goodbyegoodbyegoodbye... in his output string =)

    Be alert, young monk, for voodoo lurks everywhere!

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Regular Expressions and Variables
by mikfire (Deacon) on Feb 13, 2001 at 03:36 UTC
    I believe you would be interested in either quotemeta or the regex /Q/E flags. In more general terms, why are reinventing this particular wheel? Text::Template or HTML::Template (? not sure I got that one correct ) already do most of this and do it quite well.

    You get the bonus that they have already solved this problem as well as another 100 or so you haven't even thought of yet. And, because if you order before midnight tonight, you get portability for free! Anywhere there is perl and access to CPAN, your system will work.

    mikfire

      I've researched most of the template modules in CPAN, I'm working on a project where I don't know exactly what the tags are going to look like, since the system allows you to create your own, and use them any way you want. its a big level of abstraction for me :) Its almost like a templating system that makes the template system. oi.

      You'd be surprised the conditions I may have to face, Some accounts I work don't have shell access - hell I don't even have a test box of mi own.

      .

      -justin simoni
      !skazat!

Re: Regular Expressions and Variables
by lemming (Priest) on Feb 13, 2001 at 03:33 UTC
    s/\[([^\]+)]/$buz/gi; update: More flexibility is with japhy's solution.
Re: Regular Expressions and Variables
by wardk (Deacon) on Feb 13, 2001 at 03:40 UTC

    Notice you get one goodbye for every letter in "hello"

    it's doing exactly what you are asking! it's replacing every letter within the brackets (see Character Class) with the substitution string.

    Time to bone up on Regular Expressions!

    on the search....I disagree. but I suppose everyone's mileage varies...

      I understand what the reg ex was doing, I just wanteded it to do what I was saying, replace this with that. A bif of voodoo being done instead. It almost makes sense to make an extra step to allow reg ex's to honor the special reg ex syntax in variables, but hey, I didn't make the language,

      I think the search can be streamlined, I mean, why do I need to see every reply to a question? The search results will look all the same, like this:

      regular expressions and substituting in variables. * Re: regular expressions and substituting in variables. * Re: regular expressions and substituting in variables. * Re: Re: regular expressions and substituting in variables. * Re: Re: regular expressions and substituting in variables. * Re: Re: Re: regular expressions and substituting in variables. * Re: Re: Re: Re: regular expressions and substituting in variables +.

      Just give me the root one. Sometimes answers don't make sense without the question - this aint jeopardy.

       

      -justin simoni
      !skazat!

        If you're using Super Search, and only want to see root nodes, check all the section boxes except Note. All response nodes end up in the Note category. This has the advantage of allowing a search on only root nodes, but has the disadvantage of not allowing searches on root and response nodes within a specific category.

        I think the best of both worlds be having response nodes in the same category as their root nodes, with a new checkbox in Super Search for specifying root nodes only.