[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

escape a string literal for use with regex?

How can I escape a string literal easily for use as a regex with grep?


Say I wanted to implement a procmail killfile, that might, say contain a
mail subject line (dropped in with a mutt macro) like this:

Re: OT: Politics [Was:Social Contract]

(Hypothetical situation, of course...)

Then in my procmail.rc file I could do this:

# subject kill file
SUBJECT=`formail -zxSubject:`
* ? grep -i "$SUBJECT" $HOME/.mutt.killfile

This doesn't work, since $SUBJECT will contain a '[' and a ']' and these
are special.

What's the best way to be able to do something like this:

REGEXSAFESUBJECT=`$HOME/bin/regex_escape_string $SUBJECT`



David Purton
For the eyes of the LORD range throughout the earth to
strengthen those whose hearts are fully committed to him.
                                 2 Chronicles 16:9a

Attachment: signature.asc
Description: Digital signature

Reply to: