Need help defining a regex pattern

I am building a C# application and one of the requirements is that I find values that match the pattern below...

Starts with an AB or lower case ab
Has 6-9 numerical values after the aA
Ends with YZ or a lower case yz

I was expecting something like the following to work but I know it's not right because it isn't working

An example of the value I am trying to match is ab665897yz or ab6658975yz

Any help is appreciated!
J CAsked:
Who is Participating?
ozoConnect With a Mentor Commented:

pls try

Open in new window

Corrected code

Open in new window

mccarlIT Business Systems Analyst / Software DeveloperCommented:
What about if it has mixed case? i.e.  Ab123456yZ

If that should be allowed too, then try this..


Open in new window

Dirk StraussSenior Full Stack DeveloperCommented:
The pattern you want is:

Open in new window

Use Regex Storm to test your regular expressions. It's a brilliant (free) online tool.
Here is the link to your pattern, so you can see it in action and play around with the results.
Also have a look at the handy Regex Reference on the site.
Regex Storm ResultsTherefore, from your requirements:
Starts with an AB or lower case ab - (?i)(ab) matches only A or B and ignores casing
Has 6-9 numerical values after the aA - (\d[0-9]{5,8}) finds \d decimals between [0-9] 0 and 9 only if there is a minimum of 6 and a maximum of 9 numbers {5,8} in the string. I assume it's 0-based, hence the 5 and 8 instead of 6 and 9.
Ends with YZ or a lower case yz - (?i)(yz) as the beginning, case insensitive matches A or Z.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.