How should I write this regular expression string.

I am doing a few regular expressions and I am having trouble combining all the alternatives here.

I have 3 different patterns I would like to combine.

Pattern 1:
#####/AAA/YYYY

Open in new window


##### = Up to 5 digit serial number
AAA = 3-letter code can be (DEL|KOL|MUM|CHE)
YYYY = a year

My start on this is the following..
\b(IN)? ?(\d[, ]?){1,5}(\/(DEL|KOL|MUM|CHE)\/)\d{4}(\s?(B|[IP][1234]))?\b

Open in new window


2nd Pattern:
IN/PCT/YYYY/#####/AAA

Open in new window


IN = the two characters "IN"
PCT = The three characters "PCT"
YYYY = Year
##### = Up to 5 digit serial number
AAA = 3-letter code can be (DEL|KOL|MUM|CHE)

3rd Pattern:
#####/AAANP/YYYY

Open in new window

##### = Up to 5 digit serial number
AAA = 3-letter code can be (DEL|KOL|MUM|CHE)
NP = the characters "NP"
YYYY is the year.

All formats end with
(B, I1, I2, I3, I4, P1, P2, P3, P4)

Open in new window

thus you can see in my start on this
(\s?(B|[IP][1234]))?

Open in new window

which I am pretty sure covers it pretty well, but am always open to suggestions.
LVL 20
darbid73Asked:
Who is Participating?

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

x
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.

jmcgOwnerCommented:
What's the context for this? Do you know which regexp engine you have available?

For your sanity and anyone (including you) who has to look at this code a couple of months or years from now, is there any way you can accept multiple match attempts instead of rolling it all into one grand regexp?

Is it your expectation to capture parts of the match for use elsewhere in your code?

One of the things that might make this hard is distinguishing serial numbers with 4 digits from years, since in some formats the #### could precede the YYYY and in others it looks like the order could be the reverse.
ozoCommented:
((\d{1,5}/(DEL|KOL|MUM|CHE)/\d{4})|(IN/PCT/\d{4}/\d{1,5}/(DEL|KOL|MUM|CHE))|(\d{1,5}/(DEL|KOL|MUM|CHE)NP/\d{4}))(\s?(B|[IP][1234]))?

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
darbid73Author Commented:
Good questions JMCG. These formats are the possible formats of an Indian patent number.  I am currently trying to recognize patent numbers such as this.

Sanity is always important, but my main issue is I know very little about regular expressions and am worried that my end result is not very efficient.

I will be using a .NET engine.

I want it to find the whole number, the only thing that is optional is the kind code on the end.  So all these combinations are possible....

IN 56987/KOL/1999
IN56987/KOL/1999 I2
IN/PCT/2004/96545/KOL
IN/PCT/2004/96545/KOL B
IN 54565/DELNP/2015
IN54565/DELNP/2015P3

I have just noticed that my original question does not have "IN" at the beginning of all of them. I am going to make this a rule that "IN" must be at the beginning.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

darbid73Author Commented:
Under my small change that an IN is at the beginning and with the help of OZO I have this

IN ?(\d{1,5}\/(DEL|KOL|MUM|CHE)(NP)?\/\d{4})|(IN\/PCT\/\d{4}\/\d{1,5}\/(DEL|KOL|MUM|CHE))(\s?(B|[IP][1234]))?

Open in new window

MlandaTCommented:
I would also suggest a tool like Expresso (http://www.ultrapico.com/expresso.htm). It helps with documentation of the RegEx and easy testing.View from Expresso
jmcgOwnerCommented:
That's quite a nice tool! I note that you missed the space in front of the first question mark.

What does a yellow check mark signify?
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.