dgs2001
05-05-2014, 02:29 AM
I am using string manipulation to return Text from a website.
p1 = InStr(1, tmpSTR, "<optgroup label=""United Kingdom"">") + 33
p2 = InStr(p1, tmpSTR, "</optgroup>")
tmpSTR = Trim(Mid(tmpSTR, p1, p2 - p1))
The above code works fine but later on gets messy when i start having to loop through it to retrieve other text.
p3 = InStr(p1, tmpSTR, ":") t = Trim(Mid(tmpSTR, p3 - 2, 5))
Do
p3 = p3 - 1
Loop Until Mid(tmpSTR, p3, 1) = ">"
p1 = p3
Do
p1 = p1 - 1
Loop Until Mid(tmpSTR, p1 - 1, 1) = "/"
id = Trim(Mid(tmpSTR, p1 - 7, 6))
what I am trying to do is learn RegEx. This is my first attempt but its not working and I'm a bit stuck.
I have tried to write a custom function to return the text between two other strings.
my idea is to pass the main string and the start and end strings of the section I need.
Function txtBetween(str As String, strStart As String, strEnd As String) As String
Dim anstxt As String, re As New RegExp
Dim matches As MatchCollection
re.Pattern = "/(?m)" & strStart & ".*?" & strEnd & "/"
Set matches = re.Execute(str)
End Function
This is my calling code
tmpSTR = txtBetween(tmpSTR, "<optgroup label=""United Kingdom"">", "</optgroup>")
but it fails on this line
Set matches = re.Execute(str)
Can anybody help?
Thanks
p1 = InStr(1, tmpSTR, "<optgroup label=""United Kingdom"">") + 33
p2 = InStr(p1, tmpSTR, "</optgroup>")
tmpSTR = Trim(Mid(tmpSTR, p1, p2 - p1))
The above code works fine but later on gets messy when i start having to loop through it to retrieve other text.
p3 = InStr(p1, tmpSTR, ":") t = Trim(Mid(tmpSTR, p3 - 2, 5))
Do
p3 = p3 - 1
Loop Until Mid(tmpSTR, p3, 1) = ">"
p1 = p3
Do
p1 = p1 - 1
Loop Until Mid(tmpSTR, p1 - 1, 1) = "/"
id = Trim(Mid(tmpSTR, p1 - 7, 6))
what I am trying to do is learn RegEx. This is my first attempt but its not working and I'm a bit stuck.
I have tried to write a custom function to return the text between two other strings.
my idea is to pass the main string and the start and end strings of the section I need.
Function txtBetween(str As String, strStart As String, strEnd As String) As String
Dim anstxt As String, re As New RegExp
Dim matches As MatchCollection
re.Pattern = "/(?m)" & strStart & ".*?" & strEnd & "/"
Set matches = re.Execute(str)
End Function
This is my calling code
tmpSTR = txtBetween(tmpSTR, "<optgroup label=""United Kingdom"">", "</optgroup>")
but it fails on this line
Set matches = re.Execute(str)
Can anybody help?
Thanks