|
-
Mar 2nd, 2009, 08:32 AM
#1
Thread Starter
Addicted Member
[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(src, sPattern).Item(0).Groups(1).Value)
But it doesn't work.
Please help
-
Mar 2nd, 2009, 09:21 AM
#2
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
-
Mar 2nd, 2009, 10:13 AM
#3
Thread Starter
Addicted Member
Re: [2008] Regex/parsing help
 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.
-
Mar 2nd, 2009, 10:30 AM
#4
Lively Member
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=", "&")
-
Mar 2nd, 2009, 10:39 AM
#5
Re: [2008] Regex/parsing help
 Originally Posted by n00b scripter
Thx! Works great, but how to put it in a variable?
Hey,
Did you get it working?
Gary
-
Mar 2nd, 2009, 11:36 AM
#6
Thread Starter
Addicted Member
Re: [2008] Regex/parsing help
 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!
-
Mar 2nd, 2009, 11:40 AM
#7
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
-
Mar 2nd, 2009, 11:46 AM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] [2008] Regex/parsing help
 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
-
Mar 2nd, 2009, 11:52 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|