JoeS.
JoeS.

Reputation: 73

Regex to validate characters before period are A-Z and after only one space

I need a regex that characters before period (if it is there) are 'A' through 'Z' or 'a' through 'z', and after the only valid character is a space (possibly followed by more characters). Therefore the following should match:

Mr. Smith
SMITH MR. (space here)
dr. Jones MD. (space here)
Guy Here
HERE GUY

I am currently trying to use the following regex:

([a-zA-Z]*\.\s)[a-zA-Z]*

Not sure where to go from here.

Upvotes: 0

Views: 322

Answers (1)

Bohemian
Bohemian

Reputation: 425083

Match either not a dot, or a dot surrounded by the appropriate stuff:

^([^.]|(?<=[a-zA-Z])\.(?= ))*$

See live demo.

Breaking it down:

  • ^ start
  • [^.] non-dot
  • (?<=[a-zA-Z]) previous char was a letter
  • \. a dot
  • (?= ) next char is a space
  • $ end

And there's an alternation there (a|b)* means "any number of a or b".

Upvotes: 1

Related Questions