Results 1 to 5 of 5

Thread: [2005] help with parsing

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    [2005] help with parsing

    what is the best way to parse out items in a textbox?

    i need to capture all the values between []

    example:

    Hello [name]

    Blah blah blah [place]

    Thank you.
    [user]



    This is what i'm trying with no luck
    Code:
            Dim sMsgBody As String = Me.TextBox1.Text
            Dim split As String() = sMsgBody.Split(New [Char]() {"["c, "]"c})
    
            Dim s As String
            For Each s In split
                If s.Trim() <> "" Then
    
                    Console.WriteLine(s)
    
                End If
            Next s

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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))
    Last edited by Bulldog; Aug 16th, 2007 at 02:29 PM.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    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?

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

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