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

Hi Monks :-)

I have another regex question: I have a small and a large text. The small text is just a part of the larger one.

For example:

$text1 = 'A company (IBM) made a profit of $300 in the current year.' +; $text2 = 'Latest profit news: A company (IBM) made a profit of $300 i +n the current year. This is 180% more than in the last year.';

I'd like to replace the small text in the larger one. Now my problem is that the smaller text contains various characters that are used in regexes "()$." and that this regex
$text2 =~ s/($text1)/FOO$1BAR/;
doesn't seem to work ...

Is there any workaround ?

Many thanks for your help !

Dany

Replies are listed 'Best First'.
Re: Another (maybe stupid) regex question ...
by !1 (Hermit) on Nov 17, 2003 at 15:09 UTC
    $text2 =~ s/(\Q$text1\E)/FOO$1BAR/;

    perldoc perlre for more information. I hope this helps you with your problem.

      OMG, not again :-) Damn i really should read this great doc a bit more ... That's the second time today i could have found the answer in 'perlre'.

      But hey, thanks a lot !

Re: Another (maybe stupid) regex question ...
by EvdB (Deacon) on Nov 17, 2003 at 16:54 UTC
    TIMTOWTDI: This is also an acceptable solution, which simply does the quoting outside the regex:
    $text1 = quotemeta( $text1 ); $text2 =~ s/($text1)/FOO$1BAR/;

    --tidiness is the memory loss of environmental mnemonics

Re: Another (maybe stupid) regex question ...
by Melly (Chaplain) on Nov 17, 2003 at 15:22 UTC

    You need to escape the relevant characters - luckily you don't have to roll your own escaper, just use \Q and \E -

    $text2 =~ s/(\Q$text1\E)/FOO$1BAR/;

    BTW you describe one thing (replace small text), but then do another (put FOO and BAR on either side of small text) - did you mean /FOOBAR/ or /FOO$1BAR/?

    ABTW I'm not convinced there are any stupid questions, just stupid answers (plus a lot of inquisitive idiots ;)

    Tom Melly, tom@tomandlu.co.uk
      ABTW I'm not convinced there are any stupid questions

      This depends on what you mean by "stupid". (The word is used in so many different ways...) There are certainly such things as uninformed questions. Most "normal" questions could be considered in this category (as opposed to didactic and rhetorical questions, though those can sometimes be uninformed as well, but that's another topic). Also there are questions that don't lead directly to the answer that is desired (though often they lead to other questions that may get there eventually). Then there are questions raised only to cause trouble, not out of any desire to learn. Any of could be considered "stupid" in one sense or another of that term, though only the last category ought perhaps not to be asked.


      $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

        Heh - or to put it another way, there aren't any smart questions...

        Either you ask a question because you don't know the answer - how dumb can that be? - or you ask a question when you already know the answer - even dumber - or you ask a question to provoke and annoy - back of the class for you....

        Tom Melly, tom@tomandlu.co.uk