【发布时间】:2017-07-07 15:40:32
【问题描述】:
我正在使用一个广泛使用 VBScript 的产品,我需要解析一个字符串来提取特定数据。我知道如何用其他语言做到这一点,但需要用 VBScript 指出正确的方向。
这是我从文本文件中读取的字符串。
<PGConfiguration GPOName="Production Policy" GPODisplayName="Production Policy" GPOVersion="0" />
需要提取 GPOName="I_NEED_THIS_DATA" 之间的值。预计实例之间不会相同。
我有一个脚本,它使用 InStr() 来确保 GPOName= 存在并给我字符位置。我已经能够使用 Mid() 在 GPOName=" 之后开始使用它。我不知道如何找到结束报价的计数或位置。
我是否使用 InStr() 和 Mid() 走在正确的道路上,还是有更好的方法来解决这个问题(可能是正则表达式??)
此代码将打开文件,读入内容并弹出一个框,其中包含我要查找的前 10 个字符。我从这里去哪里?
谢谢!
Dim objFSO, objTextFile, Stream, strSearchFor, strFileName, strLine, strPosition
' the file to check
strFileName = "C:\checkme.txt"
' what are we looking for
strSearchFor = "GPOName="
' create the object and open the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Stream = objFSO.OpenTextFile(strFileName, 1, False)
Set objTextFile = objFSO.OpenTextFile(strFileName, 1, 0)
' read the first line with my data on it
strLine = objTextFile.ReadLine()
' do an InStr to get the position of the data
strPosition = InStr(strLine, strSearchFor)
' set the offset to 9 to get to the end of the ="
strPosition = strPosition + 9
' print of the first 10 characters after the GPOName=" match
wscript.Echo Mid(strLine,strPosition,10)
【问题讨论】:
标签: vbscript