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
- /products/dynamicCategory1/dynamicCategory2/ProductName1.aspx?id=1
- /products/dynamicCategory1/ProductName1.aspx?id=1
- /products/dynamicCategory1/ProductName1.aspx
- /products/dynamicCategory1/
- /products/dynamicCategory1
- /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