2017-07-06 86 views
0

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

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

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

需要拉动GPOName =“I_NEED_THIS_DATA”之间的值。实例之间预计不会相同。

我有一个脚本,使用InStr()来确保GPOName = exists,并给我的字符位置。我已经能够用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) 

回答

1

取决于u需要实现这个使用字符串或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 
+0

良好的通话,这正是我一直在寻找。谢谢! – OrganizedChaos

1

除了由Mithilesh提供的答案,如果你想使用正则表达式,你可以尝试以下方法:

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次或更多次(多次尽可能即? ,只要有一个匹配)

) - 捕获组结束

(=“) - 正向前查找。这意味着双引号必须跟在 之前的字符串匹配/由我们的捕获组返回。该 双引号会不会比赛

为了学习正则表达式基本的组成部分,是指对XML THIS

+0

我是一个正则表达式模式的入门者。 “(?:GPOName =”“)([^”“] +)(?=”“)” –

+0

@MithileshIndurkar你可以看到,如果你能解释在你的答案中使用的模式,这里的解释:https://regex101.com/r/HKwCIA/1 另外,如果你想练习正则表达式,这是最好的网站:) – Gurman

+0

非常感谢Kira –