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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.