Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Regular Expression for URLRewriting

  •  08-01-2007, 4:48 PM

    Regular Expression for URLRewriting

    I need a regular expression to match three different sections of a URL... I think i am close, but there are a couple of small issues and my head is starting to spin!  Any help appreciated:

    REQUIREMENT
    To split a URL into three parts:

    • Category String (may or may not be present)
    • Product String (may or may not be present)
    • Query String (may or may not be present)

    (It should be obvious from the inputs listed which section is which)

    INPUTS

     The input is a URL which has the following parts (in this order):

    • Fixed String: "/products/" (will always be present)
    • Category string (may or may not be present): "/cat1/cat2/" OR "/cat1/" OR "/cat1" (notice no trailing slash in last example)
    • Product string (may or may not be present): "productName.aspx"
    • Query string (may or may not be present): "?id=1"

     Examples

    1. /products/dynamicCategory1/dynamicCategory2/ProductName1.aspx?id=1
    2. /products/dynamicCategory1/ProductName1.aspx?id=1
    3. /products/dynamicCategory1/ProductName1.aspx
    4. /products/dynamicCategory1/
    5. /products/dynamicCategory1
    6. /products/productName.aspx

    CURRENT REGEX
    ^/products/(?<categoryString>.*)(?:/(?<productString>.*\.[^?]*)?(?:\?(?<queryString>.*))?)$


    ISSUES
    This above regular expression works for example 1 - 4 above.  However, I am having trouble getting it to work for examples 5 & 6 aswell

     

    Any help appreciated
     

    Filed under: , ,
View Complete Thread