在VBScript中提取XML节点之间数据的正则表达式

huangapple go评论54阅读模式
英文:

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:

(?&lt;=&lt;system\.serviceModel&gt;)[\s\S]*?(?=&lt;\/system\.serviceModel&gt;)

Try it.

答案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 (?&lt;=) and positive lookahead (?=)

(?&lt;=&lt;system.serviceModel&gt;)[\s\S]*?(?=&lt;\/system.serviceModel&gt;)

Example: regex101

Positive lookbehind will look for match behind a specific pattern, in this case is &lt;system.serviceModel&gt; and vice versa, positive lookahead will look ahead &lt;\/system.serviceModel&gt; 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:

&lt;system.serviceModel&gt;([\s\S]*?)(?=&lt;\/system.serviceModel&gt;)

It also work without the lookahead

&lt;system.serviceModel&gt;([\s\S]*?)&lt;\/system.serviceModel&gt;

I'm not familiar with VBScript, but the concept will be something like this:

Set regexObj = CreateObject(&quot;vbscript.regexp&quot;)
regexObj.Pattern = &quot;&lt;system.serviceModel&gt;([\s\S]*?)(?=&lt;\/system.serviceModel&gt;)&quot;
&#39; or &lt;system.serviceModel&gt;([\s\S]*?)&lt;\/system.serviceModel&gt;
Set matchesObj= regexObj.Execute(&quot;&lt;system.serviceModel&gt;Something in side the xml tag!&lt;/system.serviceModel&gt;&quot;)
If matchesObj.Count &lt;&gt; 0 Then
    WScript.Echo(matchesObj(0).Submatches(0))
    &#39; or matchesObj.Item(0).SubMatches.Item(0) 
    &#39; to access first submatch group ([\s\S]*?)
End If

huangapple
  • 本文由 发表于 2023年3月23日 09:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定