Solved

Creating a regular expression in C#

Posted on 2011-03-15
6
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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