Regular expression to match a string (1+ characters) that does NOT end in .ext (extension)

I need to test a url that it does not end with .asp

So test, test.html and test.aspx should match, but test.asp should not match.

Normally you'd test if the url does end with .asp and negate the fact that it matched using the NOT operator in code:

if(!regex.IsMatch(url)) { // Do something }

In that case the regular expression would be \.asp$ but in this case I need the regular expression to result in a match.

Background: I need to use the regular expression as a route contraint in the ASP.NET MVC RouteCollection.MapRoute extension method. The route needs to match all controllers but it should fall through when the controller in the url ends with .asp


The trick is to use negative lookbehind.

If you need just a yes/no answer:


If you need to match the whole URL:


These regexes will work with any URL where the file name occurs at the end of the URL (i.e. URLs without a query or fragment). I'm assuming your URLs fit this limitation given the regex .asp$ in your question. If you want it to work with all URLs, try this:


Or this if you want the regex to match the whole URL:


Need Your Help

ASP .Net server control events order

.net events

Which are the events of an ASP .Net server control and how does their order relate to the containing page's events?

CALayer not rendering or drawing

iphone ios cocoa-touch core-animation calayer

I have a subclassed UIButton inside a subclassed UITableViewCell. The button is loaded from a nib.