separate Firstname and lastname

i am looking for a function to parse name these pattern. I need firstname and lastname


Jenny Smith
Joe and Jim Smith
Sam Joe & Leslie Williams
Biden, Joe
James John III
Tim & Jim
Smith, Luke and Jim
VBdotnet2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
You can start by showing us what the result should be for each.

e.g. Tim & Jim => ?
0
VBdotnet2005Author Commented:
Firstname.       Lastname
jenny.              Smith
joe&jim.           Smith
Sam & leslie.    Williams
joe.                   Binden
James.              John iii
tim&jim.           ''
Luke & jim.     Smith
0
VBdotnet2005Author Commented:
I hope my sample helps.
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ZtinelCommented:
TRY THIS:
    Sub ParseName(ByVal name As String)

        Dim arr As String()
        Dim arrcount As Integer
        arr = name.Split(" ")
        arrcount = arr.Count
        For i As Integer = 0 To arr.Length - 1

        Next
        If name.Contains(",") Then
            Label2.Text = name.Substring(0, name.IndexOf(","))
            Label1.Text = name.Substring(name.IndexOf(",") + 2)
        ElseIf arr(arrcount - 2) = "&" Or _
        arr(arrcount - 2) = "AND".ToUpper Then
            Label1.Text = name
        ElseIf arr(arrcount - 1) = "I".ToUpper Or _
                arr(arrcount - 1) = "II".ToUpper Or _
                arr(arrcount - 1) = "III".ToUpper Or _
                arr(arrcount - 1) = "IV".ToUpper Or _
                arr(arrcount - 1) = "V".ToUpper Or _
                arr(arrcount - 1) = "VI".ToUpper Or _
                arr(arrcount - 1) = "JR".ToUpper Or _
                arr(arrcount - 1) = "JR.".ToUpper Then
            Label2.Text = arr(arrcount - 2) & " " & arr(arrcount - 1)
            For i As Integer = 0 To arrcount - 3
                Label1.Text = Label1.Text & " " & arr(i)
            Next
        Else
            Label2.Text = arr(arrcount - 1)
            For i As Integer = 0 To arrcount - 2
                Label1.Text = Label1.Text & " " & arr(i)
            Next
        End If

    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBdotnet2005Author Commented:
could you add this pattern also?
Jim & john Williams

result would be

firstname     lastname
jim & john      williams
0
VBdotnet2005Author Commented:
if try

jim wiliams jr.

it returns

jr as lastname
Jim as firstname
0
VBdotnet2005Author Commented:
Ztinel,

it returns as
jr as lastname
jim williams as firstname
0
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, try this:
    Sub MyParseName(ByVal fullName As String)
        Dim match As System.Text.RegularExpressions.Match =
            System.Text.RegularExpressions.Regex.Match(fullName, "^(?:(?<FirstName>(?:[a-zA-Z]+\s?)+(?:\s(?:and|&)\s(?:[a-zA-Z]+)))(?<LastName>\s[a-zA-Z]+(?:\s(?:I|II|III|IV|V|VI))?)?)$|^(?<FirstName>(?:[a-zA-Z]+))(?<LastName>\s[a-zA-Z]+(?:\s(?:I|II|III|IV|V|VI))?)$|^(?:(?<LastName>[a-zA-Z]+(?:\s(?:I|II|III|IV|V|VI))?)(?:\,\s))(?<FirstName>(?:[a-zA-Z]+)+(?:\s(?:and|&)\s(?:[a-zA-Z]+))?)$")

        Dim firstName As String, lastName As String
        If (match.Success) Then
            firstName = match.Groups("FirstName").Value
            lastName = match.Groups("LastName").Value

            MsgBox("FirstName: " & firstName & vbCrLf & "LastName: " & lastName, MsgBoxStyle.Information)
        Else
            MsgBox("Sorry I don't known how to parse this name.", MsgBoxStyle.Information)
        End If
    End Sub

Open in new window


I'm sure than it can be done in a better way using regular expressions, but it requires more work, I hope this code will be useful to you.

0
VBdotnet2005Author Commented:
yv989c,
Does it parse all of this? I have not try it.

Jenny Smith
Joe and Jim Smith
Sam Joe & Leslie Williams
Biden, Joe
James John III
Tim & Jim
Smith, Luke and Jim
jim wiliams jr.
0
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello! I did some improvements to my code:
Sub MyParseName(ByVal fullName As String)
	Dim match As System.Text.RegularExpressions.Match =
	    System.Text.RegularExpressions.Regex.Match(fullName, "^(?:(?<FirstName>(?:[A-Z]+\s?)+(?:\s(?:and|&)\s(?:[A-Z]+)))(?<LastName>\s[A-Z]+(?:\s(?:I|II|III|IV|V|VI|JR\.?))?)?)$|^(?<FirstName>(?:[A-Z]+))(?<LastName>\s[A-Z]+(?:\s(?:I|II|III|IV|V|VI|JR\.?))?)$|^(?:(?<LastName>[A-Z]+(?:\s(?:I|II|III|IV|V|VI|JR\.?))?)(?:\,\s))(?<FirstName>(?:[A-Z]+)+(?:\s(?:and|&)\s(?:[A-Z]+))?)$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

	Dim firstName As String, lastName As String
	If (match.Success) Then
	    firstName = match.Groups("FirstName").Value
	    lastName = match.Groups("LastName").Value

	    MsgBox("FirstName: " & firstName & vbCrLf & "LastName: " & lastName, MsgBoxStyle.Information)
	Else
	    MsgBox("Sorry I don't known how to parse this name.", MsgBoxStyle.Information)
	End If
End Sub

Open in new window


Try it!
0
Carlos VillegasFull Stack .NET DeveloperCommented:
YES, try it! :)
0
Carlos VillegasFull Stack .NET DeveloperCommented:
My last code of course... added support for Jr. and IgnoreCase
0
ZtinelCommented:
hi vbdotnet, I tried Jim & john Williams and jim wiliams jr. and my code worked fine.
You can test my code by adding textbox (name imput), button that calls the procedure ParseName with input parameter textbox.text and two labels that will hold first and last names. But be sure to clear the labels upon loading.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.