Reputation: 103
I want my regex expression to encompass all types of full names. ex: John F. Kennedy, J.F. Kennedy, John Kennedy, etc.
([A-Z][a-zA-Z]+)[ ]*(?:\s[^\s]+)?\s([A-Z][a-zA-Z]+)
I wrote this initially, but i noticed that J.F. Kennedy would not be included. I want all types of names to be found.
Would appreciate it if someone could help me solve this, thank you!
Also, is there a difference between [ ]*
and \s
?
Upvotes: 0
Views: 493
Reputation: 912
Depending on your definition of "name", the result may vary.
One possibility:
This example leads to the regex (?:[A-Z](?:\.|[a-z]+ )\s*)+[A-Z][a-z]+
If you want the surname to be abbreviated, leave the last part away.
If you want to allow '`-
in the names, insert them.
Upvotes: 1
Reputation: 1917
You can try this regex: /^([A-Z]([a-z]+|\.)\s*){2,3}$/
It specifies that each part of the name must start with an uppercase letter followed by either a dot or lowercase letters, and there can be from 2 to 3 parts in a name.
Demo: https://regex101.com/r/cfCquW/2
Upvotes: 1