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
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.
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.
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.
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)([#?].+|$)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With