BlackM
BlackM

Reputation: 4065

Regular Expression issue with specific string

I have to parse an inconsistence string and these are the formats of the strings:

1SURNAME/NAMEMR (The last two or three chars are MR/MRS/MS/DR)
1SURNAME/NAME MR
or
1SURNAME/NAME

I need to catch this sequence using Regular Expression and I have built this one: 1[A-Z]*\/[A-Z]*[\s]?[[MRS|MR|MS|DR]+

but for this name it works only for:

1SMITH/GEORGEMR
1SMITH/GEORGE MR

but not for 1SMITH/GEORGE

Anyone knows what is going wrong here?

Upvotes: 0

Views: 27

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174776

Put the last part into a non-capturing group and make it as optional by adding a ? quantifier next to that group.

\b1[A-Z]*\/[A-Z]*\s?(?:MRS|MR|MS|DR)?\b

DEMO

Upvotes: 1

Related Questions