Solved

Creating a regular expression in C#

Posted on 2011-03-15
6
455 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now