Solved

I need help parsing a string and placing it into 3 separate variables

Posted on 2014-04-16
8
212 Views
Last Modified: 2014-05-11
Hi Experts,
I need help parsing a string in my VB6 application.  I have a list of names formatted in the following manner:  

LAST NAME, FIRST NAME Middle Initial
Jones, Martha
Smith, Olivia R


I want to parse  the name so that the LAST NAME, FIRST NAME, and Middle Initial are placed in the following 3 variables:  strLastName, strFirstName, strMiddleInit (if it is there).  How can I do this?


Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
8 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 167 total points
Comment Utility
This should come close
Dim oRE as object
Dim oMatches as object
Dim oM as object      'only needed if passing a lot of names at once
Dim strWholeName as string
Dim strLastName as string, strFirstName as string, strMiddleInit as string

set oRE = createobject("vbscript.regexp")
oRE.Global = True
oRE.pattern = "(.*?), ([^ ]*) ?(.*)"
strWholename = "Jones, Martha"
if oRE.test(strWholename) THen
   set oMatches = oRE.Execute(strWholename)
   strLastName = oMatches(0).submatches(0)
   strFirstName = oMatches(0).submatches(1)
   strMiddleInit = oMatches(0).submatches(2)
End If

strWholename = "Smith, Olivia R"
if oRE.test(strWholename) THen
   set oMatches = oRE.Execute(strWholename)
   strLastName = oMatches(0).submatches(0)
   strFirstName = oMatches(0).submatches(1)
   strMiddleInit = oMatches(0).submatches(2)
End If

Open in new window

0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 167 total points
Comment Utility
Here is a way with a bit less code:
   
Dim strParts() As String
    Dim strSurname  As String
    Dim strFirstName  As String
    Dim strMiddle  As String
    
    strParts = Split(Replace(strFullName, ",", ""), " ")
    strSurname = strParts(0)
    strFirstName = strParts(1)
    If UBound(strParts) > 1 Then
        strMiddle = strParts(2)
    End If

Open in new window

0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 166 total points
Comment Utility
Here's some strait forward code doing simple InStr searches and basic Left, Mid, and Len functions:
Dim I as Integer
I = InStr( strFullName "," )
If I > 0 Then
    strLastName = Left$( strFullName, I - 1 )  'Save Last Name    

    strFullName = Trim$(Mid$( strFullName, I + 1 ))  'Update Name to just 1st and MI

    I = InStrRev( strFullName, " " )
    If I > 0 Then
        strMiddleInit = Mid$( strFullName, I + 1 )

        'Check for double 1st names (i.e. name with a space, yet no middle initial
        If Len( strMiddleInit ) > 1 Then
            strMiddleInit = ""
            strFirstName = strFullName 
        Else
            strFirstName = Left$( strFirstName, Len( strFullName ) - 2 )
        End If
    Else
        'No Middle Initial
        strFirstName = strFullName
        strMiddleInit = ""
    End If
End If

Open in new window


But the
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 45

Expert Comment

by:aikimark
Comment Utility
comment on http:#a40006099 - Graham's code
* if you have a double last name (not hyphenated), the parse will not be correct.  Think of Spanish family names.
* a correct version of Graham's code would not use the Replace() function, instead splitting on the ", " delimiter first and any remaining space delimiter second.
    strParts = Split(strFullName, ", ")
    strSurname = strParts(0)
    strParts = Split(Trim(strParts(1)), " ")
    strFirstName = strParts(0)
    If UBound(strParts) > 0 Then
        strMiddle = strParts(1)
    End If

Open in new window


comment on http:#a40006639 - HooKooDooKu's code
* if someone has a double first name, would we expect them to also have a middle initial?
* I do like your check for a single character middle initial.  It has caused me to rethink my regex pattern.  This pattern should handle multiple first names with a middle initial
^(.*?), \b(.*?)\b(.{0,1}?)$

Open in new window

It parses "briggs, joe bob d" into
Lastname=briggs
firstname=joe bob
MI=d

It parses "briggs, joe bob" into
Lastname=briggs
firstname=joe bob
MI=
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
If you need something more robust, you might give this a spin:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1819-Parsing-Names-in-MS-Office-Visual-Basic-6-and-Visual-Basic-for-Applications.html

It's not perfect, of course, and some edge cases will cause it to return unexpected results.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 168 points for aikimark's comment #a40005858
Assisted answer: 166 points for GrahamSkan's comment #a40006099
Assisted answer: 166 points for matthewspatrick's comment #a40008060

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now