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

Re: escape a string literal for use with regex?



David Purton wrote:
> How can I escape a string literal easily for use as a regex with grep?
> 
> e.g.,
> 
> 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:`
> :0:
> * ? grep -i "$SUBJECT" $HOME/.mutt.killfile
> /dev/null
> 
> 
> 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`
> 

I'm not certain, but did you try escaping the square-brackets directly?
 In my logcheck ignore files, I have to use something like \[.*\] to
match pids so all the different ones don't come as different hits on the
scan.

-Roberto

-- 
Roberto C. Sanchez
http://familiasanchez.net/~roberto

Attachment: signature.asc
Description: OpenPGP digital signature


Reply to: