find a location for property in a new city

Tuesday, 15 February 2011

Regex [x-y] range in reverse order

I started receiving a 'System.ArgumentException' error with a decription of 'parsing "[A-Za-z0-9_- %]+\.(bmp|jpg|jpeg|gif|png)" - [x-y] range in reverse order' after I wrote a C# Regex in my ASP.NET MVC web application.

I was using a RegularExpression Validator in my ASP.NET MVC app but it wasn't having any of it. I was looking at "A-Z" etc thinking IT IS THE RIGHT ORDER!

Turns out it is because I have a hyphen in another part of my regex expression. The one after the underscore.

Solution

Turns out hyphens have to appear at either the front or the end of the regular expression - or be escaped with a \. So now I have updated my regular expression from
[RegularExpression(@"[A-Za-z0-9_- %]+\.(bmp|jpg|jpeg|gif|png)")]
to
[RegularExpression(@"[A-Za-z0-9_ %-]+\.(bmp|jpg|jpeg|gif|png)")]

Follow britishdev on Twitter

8 comments:

  1. Thank you for posting this solution!

    ReplyDelete
  2. Awesome. Exactly what I needed.

    ReplyDelete
  3. Hannes
    Thanks!

    ReplyDelete
  4. Thank you. you saved my life

    ReplyDelete

  5. Thanks for posting this useful content, Good to know about new things here, Let me share this, . Hadoop training in pune

    ReplyDelete