Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Guys,

I've got a little lost with regular expressions in .NET 4.0. Can't understand why RegEx ignores maximum length in the pattern. See the code below...
C#
using System;
using System.Text.RegularExpressions;

namespace RegExTests
{
	class Program
	{
		static void Main()
		{
			bool result1 = Regex.IsMatch("ABCDEF", @"[A-Z]{2}");
			bool result2 = Regex.IsMatch("ABCDEF", @"[A-Z]{2,2}");

			bool result3 = Regex.IsMatch("AB", @"[A-Z]{,2}");
			bool result4 = Regex.IsMatch("AB", @"[A-Z]{2,2}");
		}
	}
}

What I need to achieve, is to parse a country code that's expected to be precisely two capital letters. In the example shown RegEx understands the minimum, but completely ignores the maximum. As a result, the output is as follows:
C#
result1 = true;
result2 = true;
result3 = false;
result4 = true;


I'm not quite sure what to expect for result3, but why on earth result2 is true? I don't get it...

Please point out what I'm doing wrong there.

Thank you!
Posted
Updated 9-Jul-12 5:35am
v3
Comments
Vitaly Tomilov 9-Jul-12 11:49am    
Ok, I think I have found out already... it requires that I have ^ in the beginning and $ in the end, and then "^[A-Z]{2,2}$" works correctly. Oh well, I asked, and I answered. Thank you everybody :)

I guess it should be like this:

C#
bool result1 = Regex.IsMatch("ABCDEF", @"^[A-Z]{2}$");
bool result2 = Regex.IsMatch("ABCDEF", @"^[A-Z]{2,2}$");

bool result3 = Regex.IsMatch("AB", @"^[A-Z]{,2}$");
bool result4 = Regex.IsMatch("AB", @"^[A-Z]{2,2}$");


If a caret (^) is at the beginning of the entire regular expression, it matches the beginning of a line.

If a dollar sign ($) is at the end of the entire regular expression, it matches the end of a line.
 
Share this answer
 
Comments
Vitaly Tomilov 9-Jul-12 13:50pm    
Thank you, this is exactly what I figured out myself, shortly after asking :) To think that I used to be an expert in regular expressions, and to be stuck like this, but then I remember I only used them within Altova XML Spy, and not in Visual Studio :) Altova does not require use of ^ and & to make regular expressions work as I expected... ;)
Prasad_Kulkarni 10-Jul-12 7:19am    
+5!
If you really want to check specifically if the string is "precisely two capital letters" then Regex is overkill. This is probably much more efficient:

C#
static class ExtensionMethod
{
  public static bool IsValidCountryCodeFormat(this string str)
  {
    return str != null && 
           str.Length == 2 && 
           char.IsUpper(str[0]) && 
           char.IsUpper(str[1]);
  }
}
 
Share this answer
 
Comments
Vitaly Tomilov 9-Jul-12 13:52pm    
Thank you, but I was just looking to get to the bottom of the issue with RegEx. And I did, even before the help came :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900