Search code examples
.netregexurlregular-language

Regular Expression for matching keywords in different styles of URL's


http://mywebsite/index.aspx?db=DAYTON#id%3D7304%3Bpage%3D1%3Bview%3Dpages

http://mywebsite/#id%3D3D7304%3Bpage%3D1%3Bview%3Dpages

The two URL's above go to the exact same place but are of different styles. I am trying to write out a one line expression that will create a match no matter which style of URL is thrown at it. I have been focusing primarily on everything after the "mywebsite/"

Any help would greatly be appreciated!


Solution

  • Here's a heavy One-Liner Regex:

    ^http:\/\/mywebsite\/(index\.aspx\?db=[A-Z]+)?#((id%\w+(%3B)?)|(view%\w+(%3B)?)|(page%\w+(%3B)?))*$

    It will accept your website with an optional index.aspx?db= (set to some UPPERCASE value) and any order of the 3 variables you use: id, view and page.

    Colorful explained demo here: http://regex101.com/r/jB2jS3