Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 273
Michiel van Oosterhout Avatar asked Nov 27 '08 13:11

Michiel van Oosterhout


People also ask

What do you use in a regular expression to match any 1 character or space?

Use square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.

How do I not match a character in RegEx?

There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.

What is ?! In RegEx?

Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.


1 Answers

The trick is to use negative lookbehind.

If you need just a yes/no answer:

(?<!\.asp)$

If you need to match the whole URL:

^.*(?<!\.asp)$

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:

^[^#?]+(?<!\.asp)([#?]|$)

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

^[^#?]+(?<!\.asp)([#?].+|$)
like image 195
Jan Goyvaerts Avatar answered Oct 13 '22 08:10

Jan Goyvaerts