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

10 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
  6. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

      Delete
  7. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    Webseiten Gestaltung L├╝denscheid

    ReplyDelete