Share via


Fun with .NET Regular Expression

I got asked an interesting question by a colleague the other day...What is the best way to normalize a 5 digit range, i.e. 53574-53899?  The question seems easy enough, but in reality it's slightly harder than you would think.  Most people think that this will do the trick:

^([53574-53899])$

But, as you can see, this doesn't actually do what it looks like it should:

To properly normalize this range, you will need to break apart each digit:

^(53[5-8][7-9][4-9])$

This will give you the desired result that you are looking for:

So that is how you normalize number ranges using .NET regular expression, but that only answered the first part of their question.  There were some additional requirements that they needed to account for:

  • If the 4th digit is a 7, the 5th digit has to be a 4-9.
  • If the 4th digit is an 8 or 9, the 5th digit can be a 0-9.

So taking what we learned above, and accounting for all of the possibilities, you end up with something like this:

^(535[8,9][0-9]|53[6-8][1-6][0-9]|53[6-8][8,9][0-9]|53[5-8]7[4-9])$

This expression is actually capturing for the 4 different number ranges that make up the requirements.  Each is separated by the pipe '|', with in .NET regular expression means 'or'.

535[8,9][0-9]

This is normalizing numbers 5358x and 5359x, where x equals a digit 0-9.  This fulfills requirement #2 from above.

53[6-8][1-6][0-9]

This is normalizing numbers 53xyz, where x equals a digit 6-8, y equals a digit 1-6, and z equals a digit 0-9.  This accounts for numbers outside of requirements #1 & #2 from above.

53[6-8][8,9][0-9]

This is normalizing numbers 53xyz, where x equals a digit 6-8, y equals an 8 or 9, and z equals a digit 0-9.  This accounts for numbers outside of requirements #1 & #2 from above.

53[5-8]7[4-9]

This is normalizing numbers 53x7y, where x equals a digit 5-8 and y equals a digit 4-9.  This fulfills requirement #1 from above.

 

There are lots of ways to write normalization rules and I'm sure that there are even better ways to accomplish the above, so please feel free to comment with your examples.