Jag ska hämta två värden ifrån en annan webbplats.
Först så hämtar jag hela sidan med XMLHTTP:
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP") objXMLHTTP.Open "GET", "http://www.mindoman.se/", False objXMLHTTP.Send() v_scrape = objXMLHTTP.responseText Set objXMLHTTP = Nothing
Sen så tänkte jag köra regexp för koden för att få ut de två värdena som jag vill ha. Är detta rätt tänkt eller finns det smidigare sätt att lösa det på?
Det jag får ut då är ju HTML-koden, och mitt i all denna kod så finns följande:
var sr = new Array;
sr[0] = '7 °C';
sr[1] = '11 °C';
Jag vill ha ut 7 °C och 11 °C, hur löser man detta?
Jag är sjukt kass på patterns i regexp, någon som kan bistå med lite hjälp?
Dim re, myMatches, myMatch
Set re = New RegExp
re.IgnoreCase = True
re.MultiLine = True
re.Pattern = "sr\[\d+\] = '([\s\S]*?)';"
Set myMatches = re.Execute(SubjectString)
For Each myMatch In myMatches
Response.Write myMatch.SubMatches(0) & "<br />" ' 7°C and 11 °C
Next
Dim re, myMatches, myMatch
Set re = New RegExp
re.IgnoreCase = True
re.MultiLine = True
[b]re.Global = True[/b]
re.Pattern = "sr\[\d+\] = '([\s\S]*?)';"
Set myMatches = re.Execute(SubjectString)
For Each myMatch In myMatches
Response.Write myMatch.SubMatches(0) & "<br />" ' 7°C and 11 °C
Next
Dim re, myMatches, myMatch
Set re = New RegExp ' Creates the Regular Expression Object
re.IgnoreCase = True ' We dont want to worrie about case sensitive content
re.MultiLine = True ' The search will/can be on more lines
re.Global = True ' All Results, or just the first result
re.Pattern = "sr\[\d+\] = '([\s\S]*?)';" ' * Look at Comments below
Set myMatches = re.Execute(SubjectString) ' Get all Matches
For Each myMatch In myMatches ' Loop the matches
' Print the Matches:
Response.Write myMatch.SubMatches(0) & "<br />" ' 7°C and 11 °C (backreference number 1)
Next
Set Re = Nothing
Set myMatches = Nothing
' * RegExp RegExp Buddy Explains ******************
' sr\[\d+\] = '([\s\S]*?)';
'
' Match the characters “sr” literally «sr»
' Match the character “[” literally «\[»
' Match a single digit 0..9 «\d+»
' Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
' Match the character “]” literally «\]»
' Match the characters “ = '” literally « = '»
' Match the regular expression below and capture its match into backreference number 1 «([\s\S]*?)»
' Match a single character present in the list below «[\s\S]*?»
' Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
' A whitespace character (spaces, tabs, line breaks, etc.) «\s»
' Any character that is not a whitespace character «\S»
' Match the characters “';” literally «';»