如何使用VBScript在同一文件中使用两个图案并显示?
我的代码如下:如何使用VBScript在同一文件中使用两个图案并显示?,vbscript,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.Fi
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
选项显式
Dim myURL、oXMLHttp、objFSO、描述、write2文件、ws
myURL=”https://www.cbsnews.com/latest/rss/main"
设置ws=CreateObject(“wscript.shell”)
设置oXMLHttp=CreateObject(“MSXML2.XMLHTTP”)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
oXMLHttp.Open“GET”,myURL,False
发送
如果oXMLHttp.Status=200,则
Description=Extract(oXMLHttp.responseText)
Set write2File=objFSO.CreateTextFile(“.\Description.txt”,True)
write2File.WriteLine(说明)
write2File.Close
如果结束
函数提取(数据)
暗淡的、匹配的、匹配的
Set re=New RegExp
re.Global=True
re.IgnoreCase=True
re.MultiLine=True
re.Pattern=“([\s\s]*?)”
设置匹配项=重新执行(数据)
比赛中的每一场比赛
描述=描述和匹配。子匹配(0)&vbCrLf&vbCrLf
下一个
摘录=描述
端函数
现在我需要在同一个文本文件中用两种不同的模式保存标题和描述。例如:
re.Pattern = "<title>([\s\S]*?)<\/title>" 'pattern 01
re.Pattern = "<description>([\s\S]*?)<\/description>" 'pattern 02
re.Pattern=“([\s\s]*?)””模式01
re.Pattern=“([\s\s]*?)””模式02
如何将其保存在文本文件中(示例):
第01行:标记“标题”之间的文本
第02行:标记“描述”之间的文本
第03行:标记“标题”之间的文本
第04行:标记“说明”之间的文本
等
我在另一个For
中尝试了一个For
,但结果并不像预期的那样,因为我想我遗漏了一些东西 使用替代选项:
re.Pattern=“([\s\s]*?)|([\s\s]*?)”
并附加相应的子匹配:
如果不是IsEmpty(Match.SubMatches(0)),则
描述=描述和匹配。子匹配(0)
否则如果不是空的(匹配子匹配(1)),则
描述=描述和匹配。子匹配(1)
如果结束
您需要读取和解析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
函数提取(数据)
Set doc=CreateObject(“MSXML2.DOMDocument”)
doc.loadXML(数据)
如果doc.parseError为0,则
response.write doc.parseError.reason
答复:完
如果结束
Description=“”
对于文档中的每个节点,选择节点(“/rss/channel/item”)
如果不是node.selectSingleNode(“title”)则为空
Description=描述和节点。选择单个节点(“标题”)。text&vbCrlf&vbCrlf
如果结束
如果不是node.selectSingleNode(“description”)则为空
Description=说明和节点。选择SingleNode(“说明”)。text&vbCrlf&vbCrlf
如果结束
Description=Description&vbCrlf&vbCrlf
下一个
摘录=描述
端函数
谢谢您的回复!我已经测试了修改后的代码,效果非常好。
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