I am designing a regex to use in some IIS Url Rewrites. The intent is to capture urls which:
- Are not just a file (as identified by containing a period) in the root directory, and
- Do not contain a querystring, and
- Do not belong to a specific set of sub-directories, specifically “Account” and “Public”
My current regex looks like:
^(?!(Account)|(Public))([^./]+)(/[^?]*)?$
Using RegexPal with the test set of:
file.aspx
Account/otherfile.aspx
Public/otherfile.aspx
otherfolder1/otherfile.aspx?stuff=otherstuff
otherfolder2/otherfolder/otherfile.aspx
otherfolder3/
otherfolder4
My regex correctly ignores the first two cases, but it is still matching on the third case. What is wrong with the lookahead here?
As reported by sln, the problem with these tests in RegexPal is that running a multi-line test enables multiple lines to group together to create a single match when they otherwise shouldn’t.
The regex is fine for the purposes that it is designed to fulfill. It’s actually overkill. For IIS Rewrites and Redirects, if you are using the IIS URL Rewrite Module, you have the option of specifying conditions on which it will or will not accept matches. Some of those options include:
These will achieve the desired effect more completely than the negative-lookahead.