Re: [2005] help with parsing
Here you go...
Code:
Option Strict On
Imports system.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim matches As MatchCollection = Regex.Matches(TextBox1.Text, "\[.*?\]")
Dim TextItems As New ArrayList
For i As Integer = 0 To matches.Count - 1
TextItems.Add(matches(i).ToString.Replace("[", String.Empty).Replace("]", String.Empty))
MessageBox.Show(TextItems(i).ToString)
Next
End Sub
End Class
If the text items can be split over multiple lines then use;
Code:
Dim matches As MatchCollection = Regex.Matches(TextBox1.Text, "\[.*?\]", RegexOptions.Singleline)
and also use
Code:
TextItems.Add(matches(i).ToString.Replace("[", String.Empty).Replace("]", String.Empty).Replace(ControlChars.NewLine, String.Empty))
Re: [2005] help with parsing
You can use Regex.
Try this
Code:
Dim input As String = "Hello [Mike], how[are]you doing[?]"
Dim pattern As String = "(?<=\[).*?(?=\])"
Dim matches As System.Text.RegularExpressions.MatchCollection
matches = System.Text.RegularExpressions.Regex.Matches(input, pattern)
For Each m As System.Text.RegularExpressions.Match In matches
MessageBox.Show(m.Value)
Next
Re: [2005] help with parsing
in the pattern (?<=\[).*?(?=\])
the items between ) ( are not skipped?
start: (?<=\'Start Value')
end: (?=\'End Value')
ignore: ) ignore contents (
am i close to understanding this?
Re: [2005] help with parsing
(?<=XXX) means to include this prefix (XXX) in the search but do not add it to the result.
(?=XXX) means to include this suffix (XXX) in the search but do not add it to the result
So the end result is what in between the prefix and suffix.
The \ is just the escape character since the squared brackets are special characters used by regex, so you have to use the escape char to let it know that it should treat the [ and ] as literals.