|
-
Jan 27th, 2012, 01:57 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Regex is nothing ?
Hi, i have a regex below that works great
Code:
Dim Regex As New System.Text.RegularExpressions.Regex("<div><strong>You have (.*?)</a></div>")
Dim matches As MatchCollection = Regex.Matches(thepage)
For Each match As Match In matches
TextBox3.Text = ("You have " & match.Groups(1).Value)
Next
What i need to know is how to check if it returns nothing?
For example the above code lets me know if i have a private message, if i have then it tells me, if i have not then the regex wont grab anything.
In the source, if i have a message it will say "You have whatever"
If i do not then it wont have anything in the source.
I have tryed things like:
If matches.ToString = nothing
If matches.ToString = ""
but they don't work :/
Thanks for any help
-
Jan 27th, 2012, 02:52 PM
#2
Re: Regex is nothing ?
Just check for the number of matches:
Code:
Dim Regex As New System.Text.RegularExpressions.Regex("<div><strong>You have (.*?)</a></div>")
Dim matches As MatchCollection = Regex.Matches(thepage)
If matches.Count = 0 Then
'No matches
Else
For Each match As Match In matches
TextBox3.Text = ("You have " & match.Groups(1).Value)
Next
End If
Of course, since you're only using one in the first place, you can use Match instead:
Code:
Dim re As New Regex("<div><strong>You have (.*?)</a></div>")
Dim m As Match = re.Match(thepage)
If m.Success Then
Me.TextBox3.Text = "You have " & match.Groups(1).Value
Else
Me.TextBox3.Text = "No new messages."
End If
-
Jan 27th, 2012, 03:14 PM
#3
Thread Starter
Hyperactive Member
Re: Regex is nothing ?
Thanks,
If m.Success did not work but If matches.Count = 0 worked 
Well i changed it to If matches.Count > 0 with an else statement.
Thanks
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
|