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

Re: escape a string literal for use with regex?



On Tue, May 02, 2006 at 03:01:02AM -0400, Roberto C. Sanchez wrote:
> 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.
> 

I need something automatic though. I have no control on what formail
returns. It just returns the subject as a string. I need to be able to
search for that string in the killfile just using literals and not
caring about regex.

cheers

dc

-- 
David Purton
dcpurton@chariot.net.au
 
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: