Solved

Creating a regular expression in C#

Posted on 2011-03-15
6
456 Views
Last Modified: 2013-12-17
I would like to create a regular expression in C# for a string that has an exact length of 9 characters and the first 4 chars are alphabets (uppercase and lowercase) and last 2 chars are digits [0-9] eg. abcd00078, qwerty976
0
Comment
Question by:TAMUQITS
6 Comments
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35136907
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35136933
Regex reg = new Regex(@"(?<![A-Z0-9])[A-Z]{4}[A-Z0-9]{3}[0-9]{2}(?![A-Z0-9])", RegexOptions.IgnoreCase);

Open in new window

0
 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 35137024
try this :
            string word = "qwerty976";
            bool bMatched = Regex.IsMatch(word, @"[a-zA-Z]{4}\w{3}\d{2}");

here:
[a-zA-Z]{4} - any alphabetic character , min of 4 chars
\w {3} - any alphanumeric character, min of 3 chars
\d{2} - any digits, min of 2 digits

hope this helps.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 10

Accepted Solution

by:
Mathiyazhagan earned 30 total points
ID: 35137255
sorry ,  in above expression {n} specifies number of occurance, not min no.of occurance as I specified. That is,

[a-zA-Z]{4} - any alphabetic (of either case)  of 4 occurances
 \w {3} - any alphanumeric , of 3 occurances
\d{2} - any digits, of 2 occurances.





0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 20 total points
ID: 35144423
I think you need to anchor the pattern otherwise strings with more than 9 chars will match also.  Something like this:

Regex.IsMatch(word, @"^[a-zA-Z]{4}\w{3}\d{2}$");

0
 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 35144498
Yes Wdosanjos,You are correct. I Forgot to anchor the expression between ^  and $ , so that It only accepts exact pattern defined by expression.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question