|
-
Feb 17th, 2010, 11:20 PM
#1
Thread Starter
Member
Searching through all Regex Matches?
I'm sorry, the documentation I'm finding is really not helping me. I kind of feel like an idiot being my third post in two days but...
I'm searching through an html page and I want to put all of the Regex matches into an array? How would I go about doing this? I see the Regex.Matches but I can't quite understand how to use it an MSDN sucks when it comes to explaining it.
-
Feb 17th, 2010, 11:37 PM
#2
Re: Searching through all Regex Matches?
I see a lot of people criticising MSDN but, as a regular and long time user since I was a beginner and before .NET existed, I can say that, in most cases, it's simply that people don't use it properly. This is from the documentation for the Regex.Matches method:
The following code example illustrates the use of this method to find matches to a regular expression in a given String instance.
vb.net Code:
' Find matches. Dim matches As MatchCollection = rx.Matches(text)
Having read that you know that Matches returns a MatchCollection. Have you followed the link in the documentation to the topic for the MatchCollection class to see how to use that? That topic contains this:
The following example code illustrates the use of the MatchCollection class to interrogate a set of Match instances.
vb.net Code:
Imports System Imports System.Text.RegularExpressions Public Module Test Public Sub Main() ' Define a regular expression for repeated words. Dim rx As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _ RegexOptions.Compiled Or RegexOptions.IgnoreCase) ' Define a test string. Dim text As String = "The the quick brown fox fox jumped over the lazy dog dog." ' Find matches. Dim matches As MatchCollection = rx.Matches(text) ' Report the number of matches found. Console.WriteLine("{0} matches found in:", matches.Count) Console.WriteLine(" {0}", text) ' Report on each match. For Each match As Match In matches Dim groups As GroupCollection = match.Groups Console.WriteLine("'{0}' repeated at positions {1} and {2}", _ groups.Item("word").Value, _ groups.Item(0).Index, _ groups.Item(1).Index) Next End Sub End Module ' The example produces the following output to the console: ' 3 matches found in: ' The the quick brown fox fox jumped over the lazy dog dog. ' 'The' repeated at positions 0 and 4 ' 'fox' repeated at positions 20 and 25 ' 'dog' repeated at positions 50 and 54
What part of that is giving you trouble?
EDIT: Don't get me wrong; while this is certainly a criticism, it is definitely not an attack. I'm just saying that the information is usually there, if you look in the right place. Looking in the right place is usually best done using the index of the local MSDN library, although searching online has generally worked for me too, if the right keywords are used. Looking in the right place also often involves following one or two appropriate links from topic to topic. It's important to remember that MSDN is primarily a reference, not a tutorial, although there are various tutorial-like topics in various sections.
Last edited by jmcilhinney; Feb 17th, 2010 at 11:51 PM.
-
Feb 18th, 2010, 02:10 PM
#3
Thread Starter
Member
Re: Searching through all Regex Matches?
I appreciate the criticism. Anything that will get me to knock myself on the head and find the information on my own instead of having to post here which not only takes up my time but yours. I truley appreciate the help I have received on this forum. I have been able to decipher that code and do not foresee any problems with my using it. I do however have a standard regex problem now in terms of getting a match.
Code:
Dim responseFromServer As String = "swiftKillHandle('abcde123')"
pattern = "(?<=swiftKillHandle\(').+(?='\))"
MsgBox(Regex.Match(responseFromServer, pattern), MsgBoxStyle.OkOnly, "Error")
This does not return a match for some reason. I originally used w+ instead of .+ with the same result. I'm sure this is just something I've over-looked because I've been staring at it for so long but would be most grateful if you could tell me what I am doing wrong. Thank you again.
-
Feb 18th, 2010, 02:23 PM
#4
Re: Searching through all Regex Matches?
I know John would say this if he replied first so I'll say it for him - different questions belong in new threads. Anyway, to answer your question, your problem lies with your MsgBox line. The shared function Match in the Regex class returns a Match object, and not a string.
vb.net Code:
Dim responseFromServer As String = "swiftKillHandle('abcde123')" Dim match As Match = Regex.Match(responseFromServer, _ "(?<=swiftKillHandle\(').+(?='\))") If match.Success Then MessageBox.Show(match.Value) End If
You can see in this example that you need access the Value property of the Match to get the matched value. Which will successfully show abcde123. The code you are currently using should be throwing back an exception to you...
-
Feb 18th, 2010, 02:27 PM
#5
Thread Starter
Member
Re: Searching through all Regex Matches?
These forums are very full so I didn't want to clutter them with useless questions.
I figured it was something simple I had overlooked, I just attached the .ToString at the end and it worked, thank you.
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
|