Home > Back-end >  Regular Expression, always capital letter after "-"
Regular Expression, always capital letter after "-"

Time:03-17

I have a regular expresion like:

@"^[A-Z][a-z\-\'] ((\s[A-Z][a-z\-\'\.]*)?) (\s[A-Z][a-z\-\'\.]*)?"

which allows a name to be written like Name, Name LastName or Name LastName LastName. I added the special characters ', -,. for special names. But i want to add the condition that after the '-' the letter could be capital. But only after the dash. Nowhere else in the word, execptin the beggining. Any ideas?

CodePudding user response:

This regex would meet your dash criteria, although I can't speak for other naming variations that might be prevented.

^(?:[“"]?[A-Za-z][a-z]*["”'.,]?(-[A-Z][a-z] )?(?: (?!$))?) $

It matches the following:

Leonardo da Vinci
Leonardo di ser Piero da Vinci
Tony “The Tiger”
Tony "The Tiger"
Cynthia Ann Stephanie Lauper Thornton
Madonna
John McClane
Notorious B.I.G.
John Thomas
Mary-Kate Olsen
JP Morgan
O'Doul's Amber

It does NOT match the following:

Mortimer Snerd-
Bobby Bones-BReaker
  • Related