我怎样才能使用两种模式并使用 VBScript 在同一个文件中显示?
How can I use two patterns and show in the same file using VBScript?
我有以下代码:
Option Explicit
Dim myURL,oXMLHttp,objFSO,Description,write2File,ws
myURL = "https://www.cbsnews.com/latest/rss/main"
Set ws = CreateObject("wscript.shell")
Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set objFSO = CreateObject("Scripting.FileSystemObject")
oXMLHttp.Open "GET", myURL, False
oXMLHttp.Send
If oXMLHttp.Status = 200 Then
Description = Extract(oXMLHttp.responseText)
Set write2File = objFSO.CreateTextFile(".\Description.txt", True)
write2File.WriteLine(Description)
write2File.Close
End If
Function Extract(Data)
Dim re, Match, Matches
Set re = New RegExp
re.Global = True
re.IgnoreCase = True
re.MultiLine = True
re.Pattern = "<description>([\s\S]*?)<\/description>"
Set Matches = re.Execute(Data)
For Each Match in Matches
Description = Description & Match.SubMatches(0) & vbCrLf & vbCrLf
Next
Extract = Description
End Function
现在我需要在同一个文本文件中用两种不同的模式保存标题和描述。例如:
re.Pattern = "<title>([\s\S]*?)<\/title>" 'pattern 01
re.Pattern = "<description>([\s\S]*?)<\/description>" 'pattern 02
在文本文件中应该如何保存(示例):
line 01: Text between tag "title"
line 02: Text between tag "description"
line 03: Text between tag "title"
line 04: Text between tag "description"
etc.
我在另一个 For
中尝试了一个 For
,但结果不如预期,因为我觉得我遗漏了什么。
使用交替:
re.Pattern = "<title>([\s\S]*?)</title>|<description>([\s\S]*?)</description>"
并附加相应的子匹配项:
If Not IsEmpty(Match.SubMatches(0)) Then
Description = Description & Match.SubMatches(0)
ElseIf Not IsEmpty(Match.SubMatches(1)) Then
Description = Description & Match.SubMatches(1)
End If
您需要阅读和解析 xml 吗?
function Extract(Data)
Set doc = CreateObject("MSXML2.DOMDocument")
doc.loadXML(Data)
If doc.parseError <> 0 Then
response.write doc.parseError.reason
response.end
end if
Description = ""
For Each node In doc.selectNodes("/rss/channel/item")
if not node.selectSingleNode("title") is Nothing then
Description = Description & node.selectSingleNode("title").text & vbCrlf & vbCrlf
end if
if not node.selectSingleNode("description") is Nothing then
Description = Description & node.selectSingleNode("description").text & vbCrlf & vbCrlf
end if
Description = Description & vbCrlf & vbCrlf
Next
Extract = Description
end function
我有以下代码:
Option Explicit
Dim myURL,oXMLHttp,objFSO,Description,write2File,ws
myURL = "https://www.cbsnews.com/latest/rss/main"
Set ws = CreateObject("wscript.shell")
Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set objFSO = CreateObject("Scripting.FileSystemObject")
oXMLHttp.Open "GET", myURL, False
oXMLHttp.Send
If oXMLHttp.Status = 200 Then
Description = Extract(oXMLHttp.responseText)
Set write2File = objFSO.CreateTextFile(".\Description.txt", True)
write2File.WriteLine(Description)
write2File.Close
End If
Function Extract(Data)
Dim re, Match, Matches
Set re = New RegExp
re.Global = True
re.IgnoreCase = True
re.MultiLine = True
re.Pattern = "<description>([\s\S]*?)<\/description>"
Set Matches = re.Execute(Data)
For Each Match in Matches
Description = Description & Match.SubMatches(0) & vbCrLf & vbCrLf
Next
Extract = Description
End Function
现在我需要在同一个文本文件中用两种不同的模式保存标题和描述。例如:
re.Pattern = "<title>([\s\S]*?)<\/title>" 'pattern 01
re.Pattern = "<description>([\s\S]*?)<\/description>" 'pattern 02
在文本文件中应该如何保存(示例):
line 01: Text between tag "title" line 02: Text between tag "description" line 03: Text between tag "title" line 04: Text between tag "description" etc.
我在另一个 For
中尝试了一个 For
,但结果不如预期,因为我觉得我遗漏了什么。
使用交替:
re.Pattern = "<title>([\s\S]*?)</title>|<description>([\s\S]*?)</description>"
并附加相应的子匹配项:
If Not IsEmpty(Match.SubMatches(0)) Then
Description = Description & Match.SubMatches(0)
ElseIf Not IsEmpty(Match.SubMatches(1)) Then
Description = Description & Match.SubMatches(1)
End If
您需要阅读和解析 xml 吗?
function Extract(Data)
Set doc = CreateObject("MSXML2.DOMDocument")
doc.loadXML(Data)
If doc.parseError <> 0 Then
response.write doc.parseError.reason
response.end
end if
Description = ""
For Each node In doc.selectNodes("/rss/channel/item")
if not node.selectSingleNode("title") is Nothing then
Description = Description & node.selectSingleNode("title").text & vbCrlf & vbCrlf
end if
if not node.selectSingleNode("description") is Nothing then
Description = Description & node.selectSingleNode("description").text & vbCrlf & vbCrlf
end if
Description = Description & vbCrlf & vbCrlf
Next
Extract = Description
end function