Lefty
Lefty

Reputation: 391

Regular Expression and Replace

I'm fairly new to Regular Expressions and I'm really struggling with this. I can't give up though because I'm sure there must be an easy answer.

I Have a full Salutation which looks like this:

Mr A Smith

I want to convert it to Mr Smith. The trouble is that it could be MR A B SMITH or MR A B C SMITH etc. or even already MR SMITH.

For simplification assume I'm allowing for just MRS too.

I've tried ^((MR|MRS)\s)([A-Z]{1}\s)*([A-Z]{3,})$ with a Replace Pattern of $1$3 but it keeps finding each of the middle initials as $2, $3, $4 etc. I need to force it to see ANY NUMBER of Initial/Space pairs as just $2, so Surname is always $3.

I'm testing in VBScript but will be using VBA when working.

Upvotes: 1

Views: 58

Answers (1)

anubhava
anubhava

Reputation: 784898

You can search for this regex:

(MRS?)(\s+[A-Z])*\s+([A-Za-z]{3,})

And replace by:

$1 $3

RegEx Demo

Upvotes: 2

Related Questions