【问题标题】:VBScript return value in stringVBScript 以字符串形式返回值
【发布时间】: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


    【解决方案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
    

    【讨论】:

    • 很好地调用了 XML,这正是我想要的。谢谢!
    【解决方案2】:

    除了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=")([^"]+?)(?=")

    ( - 开始一个组

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

    GPOName=" - 查找子字符串 - GPOName"

    ) - 非捕获组结束

    ( - 启动一个我们想要捕获的组。注意它不是 其次是?:

    [^"] - 匹配不是双引号的字符

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

    ) - 捕获组结束

    (?=") - 正向前瞻。这意味着必须跟一个双引号 前面的字符串由我们的捕获组匹配/返回。这 双引号将不是匹配的一部分

    学习正则表达式基础知识,请参考THIS

    【讨论】:

    • 我是正则表达式模式的初学者。如果您能解释答案中使用的模式,那就太好了..... "(?:GPOName="")([^""]+)(?="")"
    • @MithileshIndurkar 你可以在这里看到解释:regex101.com/r/HKwCIA/1 另外,如果你想练习正则表达式,这是最好的网站:)
    • 非常感谢基拉?
    • @Kira ,感谢您的回答和详细解释!这是非常有价值的信息。我选择了 Mithilesh 的回答,因为我认为这在这种情况下最有意义。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2015-04-02
    • 2016-11-23
    相关资源
    最近更新 更多