Results 1 to 9 of 9

Thread: [RESOLVED] [2008] Regex/parsing help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Resolved [RESOLVED] [2008] Regex/parsing help

    I'm trying to return a unique ID from the URL with changes every time you login.
    The URL look like this: 949285 is the ID

    I tried with this regex:
    PHP Code:
            Dim sPattern As String "q=(*.?)&p"
            
    Dim src As String
            Dim net 
    As New Net.WebClient()
            
    src WebBrowser1.Url.ToString
            id 
    = (System.Text.RegularExpressions.Regex.Matches(srcsPattern).Item(0).Groups(1).Value
    But it doesn't work.

    Please help

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] Regex/parsing help

    Hey,

    Have a try with the following:

    Code:
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
            Dim inputString As String = "http://site.com/index.php?q=949285&p=news/news"
            Dim m As System.Text.RegularExpressions.Match
    
            Dim myRegex As New System.Text.RegularExpressions.Regex("(?<1>\d{6,})")
    
            m = myRegex.Match(inputString)
            While m.Success
                MessageBox.Show("Found match " & m.Groups(1).Value & " at " & m.Groups(1).Index.ToString())
                m = m.NextMatch()
            End While
        End Sub
    This matches specifically a 6 digit number.

    Hope that helps!!

    Gary

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: [2008] Regex/parsing help

    Quote Originally Posted by gep13
    Hey,

    Have a try with the following:

    Code:
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
            Dim inputString As String = "http://site.com/index.php?q=949285&p=news/news"
            Dim m As System.Text.RegularExpressions.Match
    
            Dim myRegex As New System.Text.RegularExpressions.Regex("(?<1>\d{6,})")
    
            m = myRegex.Match(inputString)
            While m.Success
                MessageBox.Show("Found match " & m.Groups(1).Value & " at " & m.Groups(1).Index.ToString())
                m = m.NextMatch()
            End While
        End Sub
    This matches specifically a 6 digit number.

    Hope that helps!!

    Gary
    Thx! Works great, but how to put it in a variable?
    Last edited by n00b scripter; Mar 2nd, 2009 at 10:16 AM.

  4. #4
    Lively Member
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    86

    Re: [RESOLVED] [2008] Regex/parsing help

    Code:
      Public Function GetBetween(ByRef strSource As String, ByRef strStart As String, ByRef strEnd As String, Optional ByRef startPos As Integer = 0) As String
            Dim iPos As Integer, iEnd As Integer, lenStart As Integer = strStart.Length
            Dim strResult As String
    
            strResult = String.Empty
            iPos = strSource.IndexOf(strStart, startPos)
            iEnd = strSource.IndexOf(strEnd, iPos + lenStart)
            If iPos <> -1 AndAlso iEnd <> -1 Then
                strResult = strSource.Substring(iPos + lenStart, iEnd - (iPos + lenStart))
            End If
            Return strResult
        End Function
    Have fun with it

    Example:
    Code:
    dim str as string = getbetween("http://site.com/index.php?q=949285&p=news/news", "?q=", "&")

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2008] Regex/parsing help

    Quote Originally Posted by n00b scripter
    Thx! Works great, but how to put it in a variable?
    Hey,

    Did you get it working?

    Gary

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: [2008] Regex/parsing help

    Quote Originally Posted by gep13
    Hey,

    Did you get it working?

    Gary
    Yeah I got it working now I used Deathader code tho. Your code worked fine but I didn't succeed to do as I wanted. But I'm sure it was I who made some misstake.

    Thx Deathader for the code! I'm sure I'll be using it for other things in the future. Much easier than a regex!

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] [2008] Regex/parsing help

    No worries.

    Regex can be a little overkill in some places, but it depends on what you are trying to do. If I understood you correctly, you wanted to extract the id as a variable...

    As you can see, in the code I posted, the id is being displayed in the MessageBox, so you should be able to use m.Groups(1).Value which would hold the id.

    Gary

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: [RESOLVED] [2008] Regex/parsing help

    Quote Originally Posted by gep13
    No worries.

    Regex can be a little overkill in some places, but it depends on what you are trying to do. If I understood you correctly, you wanted to extract the id as a variable...

    As you can see, in the code I posted, the id is being displayed in the MessageBox, so you should be able to use m.Groups(1).Value which would hold the id.

    Gary
    I tried:
    variableName = m.Groups(1).Value
    But it returned 0. But as I said, I must have done something wrong

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] [2008] Regex/parsing help

    Hey,

    Just to complete the example (and more importantly, to make sure I am not going mad ), this should give you what you want:

    Code:
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
            Dim inputString As String = "http://site.com/index.php?q=949285&p=news/news"
            Dim m As System.Text.RegularExpressions.Match
            Dim result As String
            Dim myRegex As New System.Text.RegularExpressions.Regex("(?<1>\d{6})")
    
            m = myRegex.Match(inputString)
            If m.Success Then
                result = m.Groups(1).Value
            Else
                result = "No Match"
            End If
    
            MessageBox.Show(result)
        End Sub
    Gary

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