locked
Regular Expression - How to ignore trailing slash etc RRS feed

  • Question

  • User2130758966 posted

    Hey,

    I have posted in this forum because it mentioned web.config in its description and thats where my regex is lol. I expect most people here just browse by the unanswered posts page... well thats what i do anyway.

     

    I have a problem with my regex, its for urlrewriter.net but the problem is not specific to this.

    Here is there regex:

    <rewrite url="^/Town/([^\.]+)" to="~/ViewTown.aspx?TownId=${TownLookup($1)}"/>

    now this matches:

    site.co.uk/Town/Lincoln

    but not

    site.co.uk/Town/Lincoln/

    or

    site.co.uk/Town/Lincoln?source=GooglePPC

     

    Can anyone tell me how to rewrite the query so that it ignores the extra bits (/ and ?source=GooglePPC).

     

    I really got into this regex thing a few weeks ago but now im starting to think Scott Hanselman was right when he said:

    "So you've got a problem and you've decided to use Regular Expressions to solve it. Now you've got two problems."

    Tuesday, January 13, 2009 4:25 PM

Answers

  • User-821857111 posted

    I'm not familiar with URL rewriting, but I assume that you are just trying to capture the town name, and in that case, it surely can only consist of letters?  Or possibly a url-encoded space (%20) or a hyphen? With that in mind, this should work:

    /Town/([\w20%-]+)

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 13, 2009 4:46 PM
  • User-821857111 posted

    Great.

    Incidentally, Scott Hanselman is wrong. [;)]

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2009 8:53 AM

All replies

  • User-821857111 posted

    I'm not familiar with URL rewriting, but I assume that you are just trying to capture the town name, and in that case, it surely can only consist of letters?  Or possibly a url-encoded space (%20) or a hyphen? With that in mind, this should work:

    /Town/([\w20%-]+)

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 13, 2009 4:46 PM
  • User2130758966 posted

     Thanks, that worked a treat!

    Wednesday, January 14, 2009 8:51 AM
  • User-821857111 posted

    Great.

    Incidentally, Scott Hanselman is wrong. [;)]

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 14, 2009 8:53 AM
  • User-319574463 posted

     Incidentally if you are going to use Regular Expressions, then download a free copy of Expresso from http://www.ultrapico.com/ExpressoDownload.htm

    It is excellent for developing , analysing and testing regular expressions.

    Sunday, January 18, 2009 11:32 AM