RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
(?n:(^(?(?![^,]+?,)((?<first>[A-Z][a-z]*?) )?((?<second>[A-Z][a-z]*?) )?((?<third>[A-Z][a-z]*?) )?)(?<last>[A-Z](('|[a-z]{1,2})[A-Z])?[a-z]+))(?(?=,)(, (?<first>[A-Z][a-z]*?))?( (?<second>[A-Z][a-z]*?))?( (?<third>[A-Z][a-z]*?))?)$)
Description
Regular expression for validating a person's full name. Matches on two general formats: 1) first second third last (where first, second, and third names are optional and all present are separated by a space); 2) last, first second third (where second and third are optional, last is followed immediately by a comma and a space, and second, and third, if present, are separated by a space from each other and from first). First corresponds to surname and last corresponds to family name. Each name part is captured to a named group to facilitate program manipulation. Each name part must begin with an uppercase letter, followed by zero or more lowercase letters, except for the last name. Last name must begin with an uppercase letter, followed by one or more lowercase letters, but will match exceptions formatted like the following: McD..., MacD..., O'R... Only format is validated, not spelling. NOTE: This regular expression uses positive and negative regex lookahead to determine the general format of the name, i.e. the presence or the absence of the comma determines the general format that will match. Furthermore, this initial version is not designed to accommodate titles and things like &quot;3rd&quot;.
Matches
John Paul Jones | Jones, John P | Jones
Non-Matches
Paul Jones, John | John J | Mr. John Paul Jones 3rd
Author Rating: Not yet rated. Jerry Schmersahl
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
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: manager
Name: dennis l Galloway
Date: 11/15/2013 7:47:04 PM
Comment:
test1


Title: Fine as is in c#
Name: Evan Eveland
Date: 5/10/2007 8:42:54 AM
Comment:
Works fine in c# as is. Thanks! string pattern = @"(?n:(^(?(?![^,]+?,)((?<first>[A-Z][a-z]*?) )?((?<second>[A-Z][a-z]*?) )?((?<third>[A-Z][a-z]*?) )?)(?<last>[A-Z](('|[a-z]{1,2})[A-Z])?[a-z]+))(?(?=,)(, (?<first>[A-Z][a-z]*?))?( (?<second>[A-Z][a-z]*?))?( (?<third>[A-Z][a-z]*?))?)$)"; Queue<string> tmpStrings = matchPattern(pattern, name); // using System.Text.RegularExpressions; // matchPattern; pass pattern as string, then text to match private Queue<string> matchPattern(string pattern, string text) { Queue<string> retval = new Queue<string>(); Regex r = new Regex(pattern); int[] groups = r.GetGroupNumbers(); Match m = r.Match(text); if (m.Success) { for (int i = 1; i < groups.Length; i++) { // Get the current match group.... Group cur_group = m.Groups[groups[i]]; if (cur_group.ToString() != null && cur_group.ToString() != "") { retval.Enqueue(cur_group.ToString()); } } } return retval; }


Title: Syntax Error
Name: Rick
Date: 3/15/2004 2:58:58 PM
Comment:
When it is tested in the JS mode, there is a syntax error. I am using IE6.


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials