[thelist] Reg Ex - everything except a phrase

Joshua Olson joshua at waetech.com
Thu Sep 11 08:50:31 CDT 2003


From: "Elankath, Tarun (Cognizant)" <ETarun at blr.cognizant.com>
Sent: Thursday, September 11, 2003 3:36 AM


> How about the piece of Python code below (which uses negative lookahead) ?

> r = re.compile(r"^(?:(?!\bFred\b).)*$")

A few minutes ago I pointed out that the above regex fails on a particular
case for the regex testing tool I was using.  I found another (very similar,
thanks for the idea!) regex that seems to work in the case where Fred is at
the tail end of the string:

^(?!.*\bFred\b).*$

<tip type="Filtering Hot Linked Images" author="Joshua Olson">
ISAPI_ReWrite (and perhaps other regex based prefilters) can be used to stop
some if not all hot linking of images.

RewriteCond  Referer: .+
RewriteCond  Referer: (?!http://(www\.)?mysite\.com).*
RewriteRule  .*\.(gif|jpg)  /404.asp [I,L]

This is basically saying for those requests that do pass a referer, and
where that referrer does not belong to my domain, and where the request is
requesting a file who's extension is gif or jpg, issue out the 404 page
instead of the image.

This example rule came from the product's documentation and could be
improved to include testing for querystrings, etc.
<tip>

<><><><><><><><><><>
Joshua Olson
Web Application Engineer
WAE Tech Inc.
http://www.waetech.com
706.210.0168



More information about the thelist mailing list