|
-
Oct 18th, 2010, 05:33 PM
#1
Thread Starter
Frenzied Member
[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.
-
Oct 18th, 2010, 06:03 PM
#2
Thread Starter
Frenzied Member
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
-
Oct 18th, 2010, 06:07 PM
#3
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:
Imports System.Text.RegularExpressions
Public Class Form1
Dim strList As List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strText As String = "Hi [client_name], are you are doing today? My name is [my_name]."
Dim matches() As String = Array.ConvertAll(New Regex("(?<=\[)(\w|\s)+(?=\])").Matches(strText).Cast(Of Match).ToArray, Function(m) m.Value)
strList = New List(Of String)(matches)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 19th, 2010, 07:21 AM
#4
Thread Starter
Frenzied Member
Re: [RESOLVED] Fastest way to parse text for fields
Your code is definitely cleaner. I will be using that instead. Thanks!
-
Oct 19th, 2010, 07:28 AM
#5
Thread Starter
Frenzied Member
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?
-
Oct 19th, 2010, 09:26 AM
#6
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:
Imports System.Text.RegularExpressions
Public Class Form1
Dim strList As List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
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]."
Dim matches() As String = Array.ConvertAll(New Regex("(?<=\[)(\w|\s)+(?=\])").Matches(strText).Cast(Of Match).ToArray, Function(m) m.Value)
strList = New List(Of String)((From match In matches Select match Distinct).ToArray)
Stop
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 19th, 2010, 09:54 AM
#7
Thread Starter
Frenzied Member
Re: [RESOLVED] Fastest way to parse text for fields
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|