Mutuelinvestor
Mutuelinvestor

Reputation: 3548

How does one write a regular expression that validates and corrects name strings?

I'm using pcre regular expressions and I'm trying to clean up a list names for a mailing.

I'm trying to validate and enforce the following rules:

  1. Name string start with an Upper Case initial followed by a period and space.
  2. Last follow the space are no more than 18 characters and have no white space at the end.

I've included some examples here: Example Name Strings

1. J. Doe
2. J Doe
3. J.  Doe
4. J  Doe   
5. J. Doe
  1. number 1 is a valid name string
  2. number 2 is missing the period after the first initial
  3. number 3 has more than one space following the period.
  4. number 4 has no period, two many spaces after the period and extra white space at the end.
  5. number 5 has white space at the end of the name string

The first string meets all the criteria, but all other do not meet the criteria for one reason (no period, extra spaces, etc.) or another.

Is it possible for a regular expression to take these five name string and for it to return 5 identical valid name string like number 1.

I tried using a conditional regular expression,but found that to be a dead end.

Upvotes: 1

Views: 46

Answers (1)

dawg
dawg

Reputation: 104092

For the example cases, you can do s/^\s*(\w)\w*\W+(\w+)/\1. \2/

Demo

Upvotes: 1

Related Questions