字符串

时间:2017-07-06 22:19:32

标签: vbscript

我正在使用广泛使用VBScript的产品,我需要解析字符串以提取特定数据。我知道如何用其他语言做到这一点,但需要用VBScript指向正确的方向。

这是我正在从文本文件中读取的字符串。

<PGConfiguration GPOName="Production Policy" GPODisplayName="Production Policy" GPOVersion="0" />

GPOName =&#34; I_NEED_THIS_DATA&#34;之间的值需要拉。实例之间预计不会相同。

我有一个脚本使用InStr()来确保GPOName =存在并给我字符位置。我已经能够使用Mid()在GPOName =&#34;之后启动。我不知道如何在结束报价中找到计数或位置。

我是使用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)

2 个答案:

答案 0 :(得分:1)

取决于您是否需要使用字符串或XML来实现此目的,您可以使用其中任何一个

Set objTextFile = objFSO.OpenTextFile(strFileName, 1, 0)

' read the first line with your data on it
strLine = objTextFile.ReadLine()

arr = split(strLine , "GPOName",2)

arr2 = Split(arr(1),"""")
msgbox arr2(1)

如果您可以将文件作为XML加载或只是将行加载为XML,则需要尝试,如下所示。这是一个更清洁的解决方案。

Set objXML = CreateObject("msxml2.DOMDOCUMENT")
objXML.loadXML strLine 
msgbox objXML.selectSingleNode("PGConfiguration/@GPOName").text 'Or nodevalue, whichever suites you
Set objXML = Nothing

答案 1 :(得分:1)

除了Mithilesh提供的答案外,如果您想使用Regex,您可以尝试以下方法:

Dim objFSO, objTextFile, strFileName, strTemp, objReg, objMatches
' the file to check
strFileName = "C:\checkme.txt"

' create the object and open the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFileName, 1, False)

'Read all the data, store in a variable and close the file
strTemp = objTextFile.ReadAll()
objTextFile.Close

'Regex code
Set objReg = New RegExp
objReg.Global=True
objReg.Pattern = "(?:GPOName="")([^""]+)(?="")"     'In vbscript, if we want to have a double quote inside a string, it has to be preceded with another double quote to escape it. If just one double-quote is used, it may indicate end or start of a new string.
If objReg.Test(str) Then
    Set objMatches = objReg.Execute(str)
    objMatches.Item(0).Submatches.Item(0)           'Returns the 1st match; To get all the matches run the loop to objMatches.Count
End If
Set objReg = Nothing
Set objFso = Nothing

REGEX DEMO

  

正则表达式解释:(?:GPOName =&#34;)([^&#34;] +?)(?=&#34;)

     

( - 开始一个小组

     

?: - 使当前组成为非捕获组

     

GPOName =&#34; - 查找子字符串 - GPOName&#34;

     

) - 非捕获组结束

     

( - 启动我们想要捕获的组。请注意它不是   接着是?:

     

[^&#34;] - 匹配不是双引号的字符

     

+ - 贪婪地重复前面的标记1次或多次(尽可能多次,即只要匹配)

     

) - 捕获小组结尾

     

(?=&#34;) - 积极前瞻。这意味着必须遵循双引号   我们的捕获组匹配/返回的前一个字符串。该   双引号不会是比赛的一部分

要了解正则表达式基础知识,请参阅THIS