英文:
RegEx to extract data between an xml node in VBScript
问题
Part 1:
我已经阅读了所有与上述标题匹配的参考资料,但似乎没有一个能满足我的简单需求。我必须使用一个正则表达式模式,因为我需要将其提供给第三方库进行处理。
这个正则表达式可以读取简单的 project.exe.config 文件。我已经使用的正则表达式是:
<system.serviceModel>[\s\S]*?<\/system.serviceModel>
这会读取节点及其内容,但我只需要返回内容。
Part 2:
这应该是如此简单,以至于我为在如此尊贵的论坛上提出这样的问题感到尴尬。你能推荐一个程序,可以让我在目标文件上测试各种正则表达式模式,并且同时设计用于教授我正则表达式模式的程序吗?我已经购买并安装了 RegExBuddy,但这个程序对我来说似乎太复杂了,更不用说正则表达式本身了。
编辑:
VBscript 不支持后顾断言。
我需要使用 VBscript,因为我正在使用第三方工具。
英文:
First of all I have read all the references that match the above Title but none seem to answer my simple requirement. I must use a regex pattern as I need to feed it to a third-party library for processing.
Part 1:
The xml file to read from is a simple project.exe.config file. The regex that I have already used is
<system.serviceModel>[\s\S]*?<\/system.serviceModel>
This reads the node and its contents however I ONLY need the CONTENTS to be returned.
Part 2:
This should be so simple that I am embarrassed to ask in such an esteemed forum as this. Can you recommend a program that will a) allow me to test various regex patterns on the subject file and b) be designed to tech me Regex patterns at the same time. I've paid for and installed RegExBuddy but the program itself seems to confusing for me, let alone the regex itself.
Edit:
VBscript does not support lookbehind.
I need to use VBscript as I'm using third-party tools.
答案1
得分: -1
你可以使用环视来仅捕获内容:
(?<=<system\.serviceModel>)[\s\S]*?(?=<\/system\.serviceModel>)
英文:
You can use lookarounds to only capture the content:
(?<=<system\.serviceModel>)[\s\S]*?(?=<\/system\.serviceModel>)
答案2
得分: -1
以下是翻译好的内容:
这可以通过使用正向回顾后发断言 (?<=
) 和 正向预查 (?=
) 来简单实现:
Set regexObj = CreateObject("vbscript.regexp")
regexObj.Pattern = "<system.serviceModel>([\s\S]*?)(?=<\/system.serviceModel>)"
' 或者 <system.serviceModel>([\s\S]*?)<\/system.serviceModel>
Set matchesObj= regexObj.Execute("<system.serviceModel>标签内的内容!</system.serviceModel>")
If matchesObj.Count <> 0 Then
WScript.Echo(matchesObj(0).Submatches(0))
' 或 matchesObj.Item(0).SubMatches.Item(0)
' 以访问第一个子匹配组 ([\s\S]*?)
End If
希望对你有所帮助!
英文:
That can be done quite simple using positive lookbehind (?<=
) and positive lookahead (?=
)
(?<=<system.serviceModel>)[\s\S]*?(?=<\/system.serviceModel>)
Example: regex101
Positive lookbehind will look for match behind a specific pattern, in this case is <system.serviceModel>
and vice versa, positive lookahead will look ahead <\/system.serviceModel>
pattern.
To test and learn regex, regex101.com is a good place that supports many language, has a friendly UI with explaination, and also have a nice debugger
Edit: OP edited the required to use the regex with VBScript, which doesn't support lookbehind. So the afternative is using capturing group:
<system.serviceModel>([\s\S]*?)(?=<\/system.serviceModel>)
It also work without the lookahead
<system.serviceModel>([\s\S]*?)<\/system.serviceModel>
I'm not familiar with VBScript, but the concept will be something like this:
Set regexObj = CreateObject("vbscript.regexp")
regexObj.Pattern = "<system.serviceModel>([\s\S]*?)(?=<\/system.serviceModel>)"
' or <system.serviceModel>([\s\S]*?)<\/system.serviceModel>
Set matchesObj= regexObj.Execute("<system.serviceModel>Something in side the xml tag!</system.serviceModel>")
If matchesObj.Count <> 0 Then
WScript.Echo(matchesObj(0).Submatches(0))
' or matchesObj.Item(0).SubMatches.Item(0)
' to access first submatch group ([\s\S]*?)
End If
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论