Results 1 to 5 of 5

Thread: [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    Resolved [resolved]

    Hi, it's basically a parser.

    I will be getting text from a text file and putting it into a textbox on my form for manipulation. I'll give you an example of the text I need to grab from it and what the text would look like.

    Code:
    *** Chat Log Opened: Mon Aug 02 08:53:02 2010
    
    [08:53:02]  To stop logging, type /chatlog again.
    
    [08:54:30] @@name sends, "message"
    [08:54:30] @@name sends, "message"
    
    *** Chat Log Closed: Mon Aug 02 08:54:33 2010

    The parts I've colored red and underlined are the parts I need to take from the text and nothing else. The problem is that the name part (which always appears after the "@@" will always be different in length and so will the "message" part (which appears always in quote marks, just like the "@@").

    I've never really worked with text manipulation or trying to extract certain bits of text before, so any help will be appreciated, i'm pretty sure it's a easy job but I'm not sure of the commands.

    The overall thing is, I need to search if a certain part of text is in the message part, if not move on until the next existence, if it is, grab the name.

    Cheers.
    Last edited by meight; Aug 11th, 2010 at 10:50 AM.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Getting certain text from textbox (not as simple)

    Here's a regex for you:

    Code:
    @@([\S]+)+\ssends,\s"(.+)"
    its first capturing group will correspond for name and the second one - the message

    Now the grand question. What is regex?
    RegEx comes from Regular Expressions - a very powerful tool for text search using fuzzy search criteria (like yours).
    To learn more, you can visit this site: http://www.regular-expressions.info/
    The basics about RegEx can be also read at wikipedia: http://en.wikipedia.org/wiki/Regular_expression

    And finally, to use Regular Expressions in your VB application, you will need a VS.Net regex engine which is accessible through System.Text.RegularExpressions namespace.

    Here you can find a complete syntax reference:
    http://msdn.microsoft.com/en-us/libr...cs(VS.80).aspx

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    Re: Getting certain text from textbox (not as simple)

    Quote Originally Posted by cicatrix View Post
    Here's a regex for you:

    Code:
    @@([\S]+)+\ssends,\s"(.+)"
    its first capturing group will correspond for name and the second one - the message

    Now the grand question. What is regex?
    RegEx comes from Regular Expressions - a very powerful tool for text search using fuzzy search criteria (like yours).
    To learn more, you can visit this site: http://www.regular-expressions.info/
    The basics about RegEx can be also read at wikipedia: http://en.wikipedia.org/wiki/Regular_expression

    And finally, to use Regular Expressions in your VB application, you will need a VS.Net regex engine which is accessible through System.Text.RegularExpressions namespace.

    Here you can find a complete syntax reference:
    http://msdn.microsoft.com/en-us/libr...cs(VS.80).aspx
    Ty for reply, is there another way?

    For one it looks very hard to learn, also the expression you gave me doesn't seem to work when I actually tried it.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Getting certain text from textbox (not as simple)

    The pattern given by cicatrix worked perfect when I tried it. Here's a function (that assumes you have imported the System.Text.RegularExpression namespace) that will return a List(Of String) where each string looks as follows (without the quotes) "name: message". You should easily be able to change that into any kind of output.
    Code:
      Private Function GetConversation(ByVal text As String) As List(Of String)
        Dim regex As New Regex("@@([\S]+)+\ssends,\s""(.+)""")
        Dim matches = regex.Matches(text)
        Dim list As New List(Of String)
        For Each m As Match In matches
          list.Add(m.Groups(1).Value & ": " & m.Groups(2).Value)
        Next
        Return list
      End Function

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Getting certain text from textbox (not as simple)

    The regex works fine. I prepared a sample project with this expression (VS2010)
    I used a listview to display the results.

    The code itself is very simple:

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub txtInput_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged
    6.  
    7.         Dim pattern = "@@([\S]+)+\ssends,\s""(.+)"""
    8.  
    9.         For Each m As Match In Regex.Matches(txtInput.Text, pattern)
    10.             Dim nme As String = m.Groups(1).Value
    11.             Dim msg As String = m.Groups(2).Value
    12.  
    13.             Dim lvi As New ListViewItem(nme)
    14.             lvi.SubItems.Add(msg)
    15.             lvNames.Items.Add(lvi)
    16.         Next
    17.     End Sub
    18. End Class

    Just paste your log into the left textbox (txtInput) and see the results in the listview.
    Attached Files Attached Files

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