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

Hello all,
I would like to search for text in a file and print the paragraph that contains that text.
(In the meanwhile I have been helped by you gues to use 'slurping' mode).
This text however contains characters like () * + . etc.
Instead of having to \ all 'perl patterns' I would simply like to tell perl that the text I'm trying to find a match for has to be taken as it is, without interpreting the patterns.
(This would save me a lot of work as the text to look for is dynamic, not static).
Do you know how or do you have a better idea ? Thanks, Dirk

Replies are listed 'Best First'.
Re: How to disable Pattern Matching ?
by GrandFather (Saint) on Sep 24, 2005 at 07:48 UTC

    Depends a little on your existing code, but /\Q$matchStr\E/ probably does what you want.

    Update: You might like to consider index too.


    Perl is Huffman encoded by design.
      You might like to consider index too.

      I wouldn't say "might" in this case. I'd say index() is exactly what the OP needs.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: How to disable Pattern Matching ?
by bart (Canon) on Sep 24, 2005 at 09:08 UTC
    Look at quotemeta, and its twin brother, \Q, in perlre. Like Grandfather said,
    $text =~ /\Q$search/
    is indeed one way to use it properly.

    Another, more primitive way to achieve the same effect is to precede every \W character with a backslash. That is actually exactly what quotemeta does.

    $search =~ s/(\W)/\\$1/g; # same as: $search = quotemeta($search); $text =~ /$search/ # no "\Q" tis time!
Re: How to disable Pattern Matching ?
by ikegami (Patriarch) on Sep 24, 2005 at 16:59 UTC
    Alternatively, use index instead of a regexp. It's faster.