Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:28:16+00:00 2026-06-13T02:28:16+00:00

Is there an amazing RegEx or method in C# that might achieve this for

  • 0

Is there an amazing RegEx or method in C# that might achieve this for me?

Someone types a string into a ‘Full name’ field, and I need to break it into:
Title
Firstname
Middle
Surname
Suffix

But the user can type “John Smith”, so it needs to put John into First Name, and Smith into Surname. A person can type Mr John Smith (I have a list of known titles and suffixes), so if the first string is a title, it goes into Title field.

A perfect example would be:

Mr John Campbell Smith Jr

But, they could have:

Mr and Mrs John and Mary Smith

So the title would be Mr and Mrs, the firstname would be John and Mary, and the surname is Smith (They can use either “And” or “&” as a joiner)

I’m guiessing this is too complex for regex, but I was hoping someone may have an idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:28:17+00:00Added an answer on June 13, 2026 at 2:28 am

    Alright, here is a program that I think will do the job for you. You may of course have to make some modifications because I made some assumptions based on your question, but this should certainly get you started in the right direction.

    Some of those assumptions are as follows:

    1. There is no punctuation in the name provided to the function (e.g. Jr. with the period).
    2. You must have a first and last name, but the titles, middle name, and suffix are optional.
    3. The only join operators are and and & just as stated in the question.
    4. The name is in this format {titles} {first name} {middle name} {last name} {suffix}.

    I threw a lot of different names at it, but there are certainly more possibilities, I didn’t spend any more than 30 minutes on this so it’s not fully tested.

    class Program
    {
        static List<string> _titles = new List<string> { "Mr", "Mrs", "Miss" };
        static List<string> _suffixes = new List<string> { "Jr", "Sr" };
    
        static void Main(string[] args)
        {
            var nameCombinations = new List<string>
            {
                "Mr and Mrs John and Mary Sue Smith Jr",
                "Mr and Mrs John and Mary Smith Jr",
                "Mr and Mrs John and Mary Sue Smith",
                "Mr and Mrs John and Mary Smith",
                "Mr and Mrs John Smith Jr",
                "Mr and Mrs John Smith",
                "John Smith",
                "John and Mary Smith",
                "John and Mary Smith Jr",
                "Mr John Campbell Smith Jr",
                "Mr John Smith",
                "Mr John Smith Jr",
            };
    
            foreach (var name in nameCombinations)
            {
                Console.WriteLine(name);
    
                var breakdown = InterperetName(name);
    
                Console.WriteLine("    Title(s):       {0}", string.Join(", ", breakdown.Item1));
                Console.WriteLine("    First Name(s):  {0}", string.Join(", ", breakdown.Item2));
                Console.WriteLine("    Middle Name:    {0}", breakdown.Item3);
                Console.WriteLine("    Last Name:      {0}", breakdown.Item4);
                Console.WriteLine("    Suffix:         {0}", breakdown.Item5);
    
                Console.WriteLine();
            }
    
            Console.ReadKey();
        }
    
        static Tuple<List<string>, List<string>, string, string, string> InterperetName(string name)
        {
            var segments = name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    
            List<string> titles = new List<string>(),
                firstNames = new List<string>();
            string middleName = null, lastName = null, suffix = null;
            int segment = 0;
    
            for (int i = 0; i < segments.Length; i++)
            {
                var s = segments[i];
    
                switch (segment)
                {
                    case 0:
                        if (_titles.Contains(s))
                        {
                            titles.Add(s);
                            if (segments[i + 1].IsJoiner())
                            {
                                i++;
                                continue;
                            }
    
                            segment++;
                        }
                        else
                        {
                            segment++;
                            goto case 1;
                        }
    
                        break;
                    case 1:
                        firstNames.Add(s);
                        if (segments[i + 1].IsJoiner())
                        {
                            i++;
                            continue;
                        }
    
                        segment++;
    
                        break;
                    case 2:
                        if ((i + 1) == segments.Length)
                        {
                            segment++;
                            goto case 3;
                        }
                        else if ((i + 2) == segments.Length && _suffixes.Contains(segments[i + 1]))
                        {
                            segment++;
                            goto case 3;
                        }
    
                        middleName = s;
                        segment++;
    
                        break;
                    case 3:
                        lastName = s;
                        segment++;
    
                        break;
                    case 4:
                        if (_suffixes.Contains(s))
                        {
                            suffix = s;
                        }
    
                        segment++;
    
                        break;
                }
            }
    
            return new Tuple<List<string>, List<string>, string, string, string>(titles, firstNames, middleName, lastName, suffix);
        }
    }
    
    internal static class Extensions
    {
        internal static bool IsJoiner(this string s)
        {
            var val = s.ToLower().Trim();
            return val == "and" || val == "&";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In vim, there is this amazing plugin called command-t, that lets you fuzzy-search through
There is a column that exists in 2 tables. In table 1, this column
If there is anyone that can help me understand why this git repo isn't
There's a database engine that looks amazing for a free tool and that is
I found this amazing resource http://gtmetrix.com/ that you supply a URL to a website
With jQuery UI , there is an amazing explode effect . I wonder how
There is a moment in my app, that I need to force to show
There are nice SO question and answers about this issue, but these options didn't
Are there any libraries out there that provide some limited functionality for primitive shapes.
Prescript: The amazing etherpad was recently open sourced. Get it here: http://code.google.com/p/etherpad . This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.