Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"[-+]?\b\d+\b"
This doesn't seem to grab any numbers at all. Perhaps there is more info I can provide from the script below: c.Offset(0, i + 1).Value = RegexMid(myStr, sURLdate, "bl gb") c.Offset(0, i + 2).Value = RegexMid(myStr, sURLdate, "br gb") c.Offset(0, i + 3).Value = RegexMid(myStr, sURLdate, "class=gb") Private Function RegexMid(s As String, sDate As String, sTempType As String) As String Dim re As Object, mc As Object Set re = CreateObject("vbscript.regexp") re.IgnoreCase = True re.MultiLine = True re.Global = True re.Pattern = "\b" & sDate & "/DailyHistory[\s\S]+?" & sTempType & "\D+(\d+)" If re.test(s) = True Then Set mc = re.Execute(s) RegexMid = mc(0).submatches(0) End If Set re = Nothing End Function ***Here is an example of the data from the source page that we are trying to pull from </tr </tbody <tbody <tr <td<a href="/history/airport/KSTP/2010/1/1/DailyHistory.html"1</a</td <td class="bl gb" 8 </td <td class="gb" 0 </td <td class="br gb" -8 </td Thank you! |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() I put the string into a file c:\temp\test.txt then ran this code. the pattern I was using worked. You have to move sequentially through the file to get the data. Look at what I did below. Sub UseScript() Const ReadFile = "c:\temp\test.txt" Const ForReading = 1, ForWriting = 2, _ ForAppending = 3 Set fs = CreateObject("Scripting.FileSystemObject") Set fin = fs.OpenTextFile(ReadFile, _ ForReading, TristateFalse) MyStr = fin.readall fin.Close URLdate = DateValue("1/1/2010") sURLdate = Format(URLdate, "/yyyy/d/m") Set c = Range("A1") 'find html header FindStr = "/history/airport/KSTP" & sURLdate & "/DailyHistory.html" StartSearch = 1 SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(SearchStr, FindStr) If IsNull(Results) Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If 'find Bl gb number Firstbyte = Results.Item(0).firstIndex NumberBytes = Results.Item(0).Length FindStr = "bl gb" StartSearch = StartSearch + Firstbyte + NumberBytes SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(SearchStr, FindStr) If IsNull(Results) Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If Firstbyte = Results.Item(0).firstIndex NumberBytes = Results.Item(0).Length FindStr = "(\d+|([-+]\d+))" StartSearch = StartSearch + Firstbyte + NumberBytes SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(Mid(MyStr, StartSearch), FindStr) If IsNull(Results) Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If c.Offset(0, i + 1).Value = Results.Item(0).Value 'find class=gb number Firstbyte = Results.Item(0).firstIndex NumberBytes = Results.Item(0).Length FindStr = "class=""gb""" StartSearch = StartSearch + Firstbyte + NumberBytes SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(SearchStr, FindStr) If IsNull(Results) Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If Firstbyte = Results.Item(0).firstIndex NumberBytes = Results.Item(0).Length FindStr = "(\d+|([-+]\d+))" StartSearch = StartSearch + Firstbyte + NumberBytes SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(Mid(MyStr, StartSearch), FindStr) If IsNull(Results) Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If c.Offset(0, i + 2).Value = Results.Item(0).Value 'find Br gb number Firstbyte = Results.Item(0).firstIndex NumberBytes = Results.Item(0).Length FindStr = "Br gb" StartSearch = StartSearch + Firstbyte + NumberBytes SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(SearchStr, FindStr) If Results Is Nothing Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If Firstbyte = Results.Item(0).firstIndex NumberBytes = Results.Item(0).Length FindStr = "(\d+|([-+]\d+))" StartSearch = StartSearch + Firstbyte + NumberBytes SearchStr = Mid(MyStr, StartSearch) Set Results = RegexMid(Mid(MyStr, StartSearch), FindStr) If Results Is Nothing Then MsgBox ("Did not find string : " & FindStr) Exit Sub End If c.Offset(0, i + 3).Value = Results.Item(0).Value End Sub Private Function RegexMid( _ ByVal s As String, _ ByVal FindStr) As Variant Dim re As Object, mc As Object Set re = CreateObject("vbscript.regexp") re.IgnoreCase = True re.MultiLine = True re.Global = True re.Pattern = FindStr If re.test(s) = True Then Set mc = re.Execute(s) Set RegexMid = mc Else Set RefexMid = Nothing End If Set re = Nothing End Function -- joel ------------------------------------------------------------------------ joel's Profile: 229 View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=168082 Microsoft Office Help |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Can someone help me with this regular expression? | Excel Discussion (Misc queries) | |||
Regular Expression Conditionals (?(if)then|else) in VBA? | Excel Programming | |||
Help with regular expression | Excel Programming | |||
Regular Expression | Excel Discussion (Misc queries) | |||
Regular expression searching problem | Excel Programming |