PDA

View Full Version : RegEx Newbie



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

Bob Phillips
05-05-2014, 03:50 AM
I am confused as to what is in tmpostr to start with, and why you feel you need to loop. What is wrong with Instr all the way?

Kenneth Hobs
05-06-2014, 06:33 AM
I am not well versed in regex pattern strings. Even so, as xls said, if you will show us your input string and your expected output string, we can more easily help.

Here is how I have parsed html strings in the past.

Sub Test_pGoog()
Dim s As String, sDistance As String, sDuration As String
s = "<duration>" & vbCrLf & _
"<value>16</value>" & vbCrLf & _
"<text>1 min</text>" & vbCrLf & _
"</duration>" & vbCrLf & _
"<html_instructions>Make a &lt;b&gt;U-turn&lt;/b&gt; at &lt;b&gt;Palm Dr&lt;/b&gt;&lt;div style=&quot;font-size:0.9em&quot;&gt;Destination will be on the right&lt;/div&gt;</html_instructions>" & vbCrLf & _
"<distance>" & vbCrLf & _
"<value>44</value>" & vbCrLf & _
"<text>144 ft/text>" & vbCrLf & _
"</distance>" & vbCrLf & _
"</step>" & vbCrLf & _
"<duration>" & vbCrLf & _
"<value>164061</value>" & vbCrLf & _
"<text>1 day 22 hours</text>" & vbCrLf & _
"</duration>" & vbCrLf & _
"<distance>" & vbCrLf & _
"<value>4553964</value>" & vbCrLf & _
"<text>2,830 mi</text>" & vbCrLf & _
"</distance>" & vbCrLf & _
"<start_location>"
sDistance = pGoog("distance", s)
'sDuration = pGoog("duration", s)
MsgBox sDistance
End Sub

Private Function pGoog(strSearch As String, strHTML As String) As String
Dim s As String
s = pRevTags(strSearch, strHTML)
pGoog = pRevTags("text", s)
End Function

Private Function pRevTags(strSearch As String, strHTML As String) As String
Dim s As String, p1 As Long, p2 As Long, lss As Integer
p1 = InStrRev(strHTML, "<" & strSearch & ">")
If p1 = 0 Then
pRevTags = "Not Found"
Exit Function
End If
p2 = InStrRev(strHTML, "</" & strSearch & ">")
lss = Len(strSearch)
s = Mid(strHTML, p1 + lss + 2, p2 - p1 - 2 - lss)
pRevTags = s
End Function

snb
05-06-2014, 08:40 AM
You don't need any RegEx to obtain the options


sub M_snb()
sn=split(tmpSTR, "</optgroup>")

for j=0 to ubound(sn) -1
msgbox=split(sn(j),"<optgroup ")(1)
next
end sub