2

I have a valid RegEx pattern in .NET:

(?>.*param1=value1.*)(?<!.*param2=\d+.*) which matches if:

  • query string contains param1=value1
  • but does not contain param2= a number

It works in .NET. However IIS URLRewrite complains that it is not a valid pattern.

Can I not use zero-width negative look behind (?<! ) expressions with IIS URLRewrite?

Note that I tried to apply this pattern both in web.config (properly changing < and > to &lt; and &gt; respectively, as well as in the IIS Manager - all without success.

Fit Dev
  • 3,413
  • 3
  • 30
  • 54

1 Answers1

2

IIS URLRewrite default regex syntax is ECMAScript, that is not compatible with .NET regex syntax. See URL Rewrite Module Configuration Reference:

ECMAScript – Perl compatible (ECMAScript standard compliant) regular expression syntax. This is a default option for any rule.

You cannot use a lookbehind at all, you will have to rely on lookaheads only:

^(?!.*param2=\d).*param1=value1.*

Pattern explanation:

  • ^ - start of string
  • (?!.*param2=\d) - if there is param2= followed with a digit (\d) after 0+ characters other than a newline fails the match (return no match)
  • .*param1=value1.* - match a whole line that contains param1=value1

You can enhance this rule by adding \b around param1=value1 to only match it as a whole word.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563