Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 367
Default Regular Expression Help on syntax

"[-+]?\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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Regular Expression Help on syntax


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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone help me with this regular expression? [email protected] Excel Discussion (Misc queries) 3 March 10th 09 07:36 PM
Regular Expression Conditionals (?(if)then|else) in VBA? Lazzaroni Excel Programming 7 October 8th 08 09:53 PM
Help with regular expression PO Excel Programming 3 May 2nd 07 01:39 PM
Regular Expression sl Excel Discussion (Misc queries) 2 January 23rd 07 11:57 PM
Regular expression searching problem LarryLev Excel Programming 0 September 15th 05 07:44 PM


All times are GMT +1. The time now is 05:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"