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

Re: escape a string literal for use with regex?

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";'`

Jon Dowland

Reply to: