Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 RegEx
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...
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:
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 9-Jul-12 5:26am
Edited 9-Jul-12 5:35am
v3
Comments
Vitaly Tomilov at 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 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I guess it should be like this:
 
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.
  Permalink  
Comments
Vitaly Tomilov at 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 at 10-Jul-12 7:19am
   
+5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
static class ExtensionMethod
{
  public static bool IsValidCountryCodeFormat(this string str)
  {
    return str != null && 
           str.Length == 2 && 
           char.IsUpper(str[0]) && 
           char.IsUpper(str[1]);
  }
}
  Permalink  
Comments
Vitaly Tomilov at 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
0 Sergey Alexandrovich Kryukov 588
1 George Jonsson 258
2 CPallini 200
3 OriginalGriff 174
4 Shemeemsha RA 140
0 OriginalGriff 5,985
1 Sergey Alexandrovich Kryukov 5,341
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100