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.
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.
Re: Getting certain text from textbox (not as simple)
Originally Posted by cicatrix
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.
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