Results 1 to 3 of 3

Thread: [RESOLVED] Help with extracting a team name from a line of text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    148

    Resolved [RESOLVED] Help with extracting a team name from a line of text

    I wrote a program that extracts information from posted track and field results. Here is an example:
    Event 10 55 Meter Hurdles 11-12 Division Boys
    =========================================================================
    Name Year Team Prelims Finals
    =========================================================================
    Finals
    1 Parson, Jessie I5 Elite 9.13 9.49
    2 Sampson, Zachary Unattached 11.10 9.53
    3 Drewery, Chase Fasst Track 10.37 9.66
    4 Trinh, Ty Woodlawn Tra 10.50 9.75
    5 von Chatmon, Keith Woodlawn Tra 11.00 10.78
    6 Barley Jr., Charles Sofcc Warrio 10.00 11.16


    My Attempt:

    Sub test2()
    Dim str As String
    Dim arrTmp() As String
    str = "1 Parson, Jessie I5 Elite 9.13 9.49"
    arrTmp = Split(RemoveDoubleSpace(str), " ")
    End Sub

    Public Function RemoveDoubleSpace(MyString As Variant) As String
    MyString = Trim(MyString)
    Do While InStr(MyString, " ")
    MyString = Replace(MyString, " ", " ")
    Loop
    RemoveDoubleSpace = MyString
    End Function

    This returns:
    0: 1
    1: Parson
    2: Jessie
    3: i5
    4: Elite
    5: 9.13
    6 9.49

    I want to extract "i5 Elite". For this case, I know to extract 3 &4 but the length of the team name is not known in advance. One suggestion is to determine the location of the first numerical value after the name in element (2) in hopes to get the location of 9:13 but as you can see from this example, the 5 in i5 will present a problem.

    I currently use regular expressions to extract the numerical values. I could not determine a pattern to reliably extract the team name.
    I would like to make this as general as possible.

    Thank you.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Help with extracting a team name from a line of text

    That looks more like Classic VB (VB6) code, did you post to the wrong forum or just using old code?

    FWIW, Heres a somewhat ugly solution that seems to work...
    Code:
    Dim result As String = ""
    Dim str As String = "6 Barley Jr., Charles Sofcc Warrio 10.00 11.16     "
    ' trim str (if needed)
    str = str.Trim
    ' find comma-space
    Dim commaIndex As Integer = str.IndexOf(", ") + 2
    ' find space after comma-space
    Dim startIndex As Integer = str.IndexOf(" ", commaIndex)
    ' find first char from end that is not a space, number or period
    For i As Integer = str.Length - 1 To 0 Step -1
        If Not IsNumeric(str(i)) AndAlso str(i) <> " " AndAlso str(i) <> "." Then
            ' Get sub text
            result = str.Substring(startIndex, (i + 1) - startIndex).Trim
            Exit For ' exit loop!
        End If
    Next
    ' show result = "Sofcc Warrio"
    Debug.WriteLine(result)
    Last edited by Edgemeal; Feb 24th, 2013 at 03:25 PM. Reason: declare all vars

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    148

    Re: Help with extracting a team name from a line of text

    Thank you. That worked.
    Adrian

    Quote Originally Posted by Edgemeal View Post
    That looks more like Classic VB (VB6) code, did you post to the wrong forum or just using old code?

    FWIW, Heres a somewhat ugly solution that seems to work...
    Code:
    Dim result As String = ""
    Dim str As String = "6 Barley Jr., Charles Sofcc Warrio 10.00 11.16     "
    ' trim str (if needed)
    str = str.Trim
    ' find comma-space
    Dim commaIndex As Integer = str.IndexOf(", ") + 2
    ' find space after comma-space
    Dim startIndex As Integer = str.IndexOf(" ", commaIndex)
    ' find first char from end that is not a space, number or period
    For i As Integer = str.Length - 1 To 0 Step -1
        If Not IsNumeric(str(i)) AndAlso str(i) <> " " AndAlso str(i) <> "." Then
            ' Get sub text
            result = str.Substring(startIndex, (i + 1) - startIndex).Trim
            Exit For ' exit loop!
        End If
    Next
    ' show result = "Sofcc Warrio"
    Debug.WriteLine(result)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width