Shailesh Rama
Shailesh Rama

Reputation: 158

regular expressions (three letters and three numeric)

i am having a problem creating a regular expression that will validate if the textbox has a three letters in the beginning and three numbers at the end e.g. AAA999 all 6 are required for this to be valid.

so far i have tried [A-Z][A-Z][A-Z][0-9][0-9][0-9] and [A-Z][A-Z][A-Z]\d{3}

can someone please tell me what i am doing wrong?

Upvotes: 1

Views: 4605

Answers (3)

Toto
Toto

Reputation: 91518

In order to be unicode compatible, you'd use this one:

^\pL{3}\pN{3}$

Upvotes: 0

Narendra Yadala
Narendra Yadala

Reputation: 9664

Add beginning and end anchors if you want the textbox to contain only 3 letters and 3 digits.

^[A-Z]{3}\d{3}$

Since you have C# tags, this is how code will look like in C#

Regex regexObj = new Regex(@"^[A-Z]{3}\d{3}$", RegexOptions.Multiline);
foundMatch = regexObj.IsMatch(subjectString);

Upvotes: 4

Guffa
Guffa

Reputation: 700800

Either would work, basically. Depending on how you use it, you might need to specify the start and end of the string:

^[A-Z]{3}\d{3}$

(The validation controls in .NET for example adds the starting and ending matches automatically.)

Upvotes: 3

Related Questions