- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find First Name(s) OR Last Name(s)
^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$
I wrote this regex to validate first names or last names separately. Allows for 2 names separated by space or hyphen. Will accept an abbreviated second name with period. Has to start with a letter and end with a letter or capital letter with period. Updated 3/24/2005: Now, names like McDonald work. Also, several convenient parts of the match are stored, for example: the Name MacDonald-O'Brian will yield the following: $1="MacDonald" $2="ac" $3="-" $4="O'Brian" $5="'"
O'Grady-Smith | Rachel Ann | McDonald T.
Timothy J | James Earl Jones | @#$!*!
Author Rating: Not yet rated. Timothy Dutton
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Doesn't work
Name: MG
Date: 3/31/2005 11:53:25 AM
I tested this using the following code... It returns false when using Smith-Jones or St. Louis... Private Sub ValidateName(ByVal ctl As UltraTextEditor) Dim _regexpattern As String = "^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$" If System.Text.RegularExpressions.Regex.Match(ctl.Text, _regexpattern).Success = False Then ctl.Focus() ctl.Select(0, ctl.Text.Length) Throw New ValidationException("Name is required") End If End Sub

Title: Fixed for some names that did not match
Name: Timothy Dutton
Date: 3/24/2005 12:24:06 PM
McDonald, MacDonald, DeHoyas, etc. now work

Title: Doesn't understand some names as being correct
Name: Jay
Date: 3/22/2005 2:01:37 PM
I seem to have a problem with names beginning with Mc, such as McDonald

Copyright © 2001-2018, | ASP.NET Tutorials