Solved

Creating a regular expression in C#

Posted on 2011-03-15
6
458 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon S3 Images with .Net 3 62
Upgrade code from VS 2010 to VS 2015 7 45
How does GeoJson relates to Leaflet and Mapbox? 4 48
VB .net 2010 Byte array 2 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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