tags:

views:

26

answers:

1

I've been trying to figure out how to write this regular expression. It is to be used for ISAPI_Rewrite, a module for IIS 6, for doing URL rewriting.

I want the url /hg/<parameter> to be mathed, so it can be rewrited to /hg/hgwebdir.cgi/<parameter>.

I've matched it using ^/hg/(.*).

My problem is, if the URL /hg/hgwebdir.cgi/<parameter> is used, the regex should NOT match. Using the above regex with this URL, will rewrite to /hg/hgwebdig.cgi/hgwebdig.cgi/<parameter> which is not correct.

Can you help me create the matching pattern?

+3  A: 

You could use a negative lookahead to do this:

"^/hg/(?!hgwebdir.cgi)(.*)"

http://www.regular-expressions.info/tutorial.html is a great tutorial for regular expressions. =)

Jens
Thank you very much.
Tommy Jakobsen
When looking up syntax for regexps, though, be sure to get your information for the correct regexp dialect! There are regexps in perl, java, .NET and many other languages/programs, and there are many small but significant differences. For example, the last time I needed a negative lookahead it was in vim, and the expression looked a lot different from what Jens wrote here.
Carl Smotricz
@Carl: The site I linked covers some of the quirks of different regex implementations. Not vim, though =)
Jens
To be sure, I wasn't contradicting you! I was just slapping an obligatory "here be dragons" sticker on the topic. I've occasionally been bitten by using the regexp syntax I'd learned from a different environment than the target, and wanted to save Tommy that same pain.
Carl Smotricz
@Carl, good point. I had guessed that the rewrite module in question wouldn't support look-arounds. But after looking it up, I saw it did support look-aheads (not look-behind), so I added the link to Jens' answer.
Bart Kiers