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

Hello, I am having a regex-related problem...

Basically, I want to include regex input and string literals in the same expression, without having metacharacters in the string interpreted as regex metacharacters. The code to do this looks like this...
s/ $STRING $PATTERN /$REPLACEMENT/gx # where <i>STRING</i> and <i>PATTERN</i> are user-defined
With that in mind, I was wondering if it is possible to do one of the following:

1) Is there a module for escaping regex characters in a string?
2) Is there an extension mechanism inside the syntax that allows a literal string to be included?

Thank you for any help you can provide.

Replies are listed 'Best First'.
Re: Escaping regex characters in user-defined input
by FunkyMonk (Bishop) on Aug 24, 2007 at 15:26 UTC
    Use \Q and \E around the part you want escaped. See perlre for more info

        Thank you for all of your help.