[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 11:09:30AM +0100, Jon Dowland wrote:
> At 1146586566 past the epoch, David Purton wrote:
> > How can I escape a string literal easily for use as a regex with grep?
> If you feed it through perl, you can enclose the suspect string with \Q
> and \E. Here's an excerpt from a perl script which I generate my
> procmail recipes with:
>         $reexp = $dict{$key};
> 	print ":0\n*^TO\Q$reexp\E\n\$MAILDIR/$key/\n\n";
> You could try something like
> 	TMPSUBJECT=`formail -zxSubject:`
> 	SUBJECT=`perl -e 'print "\Q$TMPSUBJECT\E";'`

I ended up using fgrep, which if I had read the grep manual I would have
found does what I want (Blush).

This works well:

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



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: