Results 1 to 3 of 3

Thread: [RESOLVED] Regex is nothing ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Resolved [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

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    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
  •  



Click Here to Expand Forum to Full Width