locked
What to confirm: RegEx test patterns do not support negative lookbehind? Any alternatives? RRS feed

  • Question

  • User-1218148251 posted

    It appears the URLRewrite 2.0 rewrite does not support a "negative lookbehind" within a regular expression. Is this true?  If they are not supported are there any alternatives?

    Example:

    If I want to match on a URL containing an 'images' directory of the form: http://www.site.com/images/someimage.gif
    And I DO NOT want to match on the URL if the string 'themes' precedes the images directory: http://www.site.com/themes/images/someimage.gif

    This can be accomplished with a regex with a negative lookbehind: ^(.*)(?<!/themes)/images/(.*)$

    This regex and variations such as: ^.*(?<!themes)/images/.*$ appear to be considering invalid syntax by the URLRewrite test pattern dialog but work as expected within tools like Regulator or JScript.

    Is there a syntax change necessary to regex in this case or is this form of regex just not supported?

    TIA.

    Friday, February 25, 2011 1:44 PM

Answers

  • User744767459 posted

    Hi,

    URL Rewrite uses the regex engine from Visual Studio C++ 2010 with the ECMA flag. You can find supported features in the following article:

    TR1 Regular Expressions
    http://msdn.microsoft.com/en-us/library/bb982727.aspx

    In this case, the pattern is not supported. You'll need to add conditions to fit your requirement, it looks like:

                    <rule name="RewriteImage" stopProcessing="true">
                        <match url="(.*)/images/(.*)$|^images/(.*)$" />
                        <conditions>
                            <add input="{URL}" pattern="(.*)/themes/images/(.*)$" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="<path>" appendQueryString="false" />
                    </rule>

    If your encounter any problem, please update here. Thanks.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, March 1, 2011 2:10 AM

All replies

  • User744767459 posted

    Hi,

    URL Rewrite uses the regex engine from Visual Studio C++ 2010 with the ECMA flag. You can find supported features in the following article:

    TR1 Regular Expressions
    http://msdn.microsoft.com/en-us/library/bb982727.aspx

    In this case, the pattern is not supported. You'll need to add conditions to fit your requirement, it looks like:

                    <rule name="RewriteImage" stopProcessing="true">
                        <match url="(.*)/images/(.*)$|^images/(.*)$" />
                        <conditions>
                            <add input="{URL}" pattern="(.*)/themes/images/(.*)$" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="<path>" appendQueryString="false" />
                    </rule>

    If your encounter any problem, please update here. Thanks.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, March 1, 2011 2:10 AM
  • User-1218148251 posted
    This worked just fine Leo.  Thanks for the documentation link and explanation.
    Saturday, March 5, 2011 6:47 AM