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

14 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. I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
    remote java developer jobs

    ReplyDelete
  6. Buy t shirt for mens online from Dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  7. Laxmi pipe Industries is leading SS Pipe manufactures . We are also manufactures and exporter of steel pipes & tubes.

    ReplyDelete
  8. Yami immigration is a well-known and experienced Best Student Visa Consultant in Surat. We provide Immigration Services for many countries such as Italy, Canada, France, Australia, Germany, USA, Malaysia, New Zealand, and Singapore.

    ReplyDelete
  9. Shreeja Health Care is leading manufacturer of Oil Maker Machine . Shreeja Oil Extraction Machine is able to extract oil from various seeds like peanuts, Coconut, Sesame, Soybean, macadamia nuts, walnuts, sunflower seeds, vegetable seeds flaxseed etc.

    ReplyDelete
  10. We SVJ Technocoat are the leading Service Provider and Exporter of an extensive array of PVD Coating Service and Vapor Deposition Coating Service etc. We are a well known firm for providing excellent quality coating services across the nation and in a timely manner. Owing to our improvised business models, our professionals are offering integrated solutions for our clients.

    ReplyDelete
  11. RQC is one of the Best FUE Hair Transplant, with services, including Hair Transplant, Hair Treatment, Hairfall and Hair Loss, dermatology, and Skincare. RQC brings you the best services in hair transplant, Hair Treatment.

    ReplyDelete