Results 1 to 7 of 7

Thread: [RESOLVED] Fastest way to parse text for fields

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Resolved [RESOLVED] Fastest way to parse text for fields

    What would be the fasted method of get a string list of all values within [] in a string of text? For example:

    Hi [client_name], are you are doing today? My name is [my_name].

    The list of strings would be:

    client_name
    my_name

    Just to be clear, I do not know beforehand what the values are between the brackets, it can be anything and any number of them.

    Visual Studio 2010

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: Fastest way to parse text for fields

    Found the following solution using RegEx. It works quite well:

    Code:
        Private Function GetFields(ByVal text As String) As List(Of String)
            Dim list As New List(Of String)
            Try
                Dim collection As MatchCollection = Regex.Matches(text, "\[([^]]*)\]")
                For Each value As Match In collection
                    list.Add(value.Groups(1).Value)
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Return list
        End Function

    Visual Studio 2010

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Fastest way to parse text for fields

    try this. it uses regex to find any text between [], + puts them in a list(of string):

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Dim strList As List(Of String)
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim strText As String = "Hi [client_name], are you are doing today? My name is [my_name]."
    9.         Dim matches() As String = Array.ConvertAll(New Regex("(?<=\[)(\w|\s)+(?=\])").Matches(strText).Cast(Of Match).ToArray, Function(m) m.Value)
    10.         strList = New List(Of String)(matches)
    11.     End Sub
    12. End Class

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] Fastest way to parse text for fields

    Your code is definitely cleaner. I will be using that instead. Thanks!

    Visual Studio 2010

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] Fastest way to parse text for fields

    One other improvement I need to make is to only add unique values. For example, if [client_name] appears more than once in the source, I just want it added once to the list. Is this easily accomplished?

    Visual Studio 2010

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Fastest way to parse text for fields

    yes. no problem. you can use LINQ to select unique names from the matches array:

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Dim strList As List(Of String)
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim strText As String = "Hi [client_name], are you are doing today? My name is [my_name].Hi [client_name], are you are doing today? My name is [my_name]."
    9.         Dim matches() As String = Array.ConvertAll(New Regex("(?<=\[)(\w|\s)+(?=\])").Matches(strText).Cast(Of Match).ToArray, Function(m) m.Value)
    10.         strList = New List(Of String)((From match In matches Select match Distinct).ToArray)
    11.         Stop
    12.     End Sub
    13. End Class

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [RESOLVED] Fastest way to parse text for fields

    Perfect! Thanks!

    Visual Studio 2010

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