vbscript 正则表达式,在两个字符串之间替换
vbscript regular expression, replace between two string
我有这个 xml:
<doc>
<ContactPrimaryEmail></ContactPrimaryEmail>
<ContactAlternateEmail></ContactAlternateEmail>
<ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile>
<ContactAlternateMobile></ContactAlternateMobile>
</doc>
我想在 VBScript 中应用正则表达式来替换 content "+00xxxxxx" 的属性 ContactPrimaryMobile,只需更改数字:
<ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile>
我是 vbscripting 的新手,我在创建对象和应用模式方面的技能有限,所以请你帮我转换这个正则表达式以在 VBScript 中使用它:
(?<=\<ContactPrimaryMobile\>)(.*)(?=\<\/ContactPrimaryMobile)
更新
我明白了:
Object doesn't support this property or method: 'Submatches'
执行时:
Dim oRE, oMatches
Set oRE = New RegExp
oRE.Pattern = "<ContactPrimaryMobile>(.*?)</ContactPrimaryMobile>"
oRE.Global = True
Set oMatches = oRE.Execute("<doc><ContactPrimaryEmail></ContactPrimaryEmail><ContactAlternateEmail></ContactAlternateEmail><ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile><ContactAlternateMobile></ContactAlternateMobile></doc>")
Wscript.Echo oMatches.Submatches(0)
首先,VBScript regex不支持lookbehinds,你需要捕获两个字符串之间的部分。
接下来,您需要通过访问 .Execute
正则表达式匹配后的匹配对象来获取 子匹配 ,并获取其 .Submatches(0)
:
Dim oRE, oMatches, objMatch
oRE.Pattern = "<ContactPrimaryMobile>(.*?)</ContactPrimaryMobile>"
然后
Set oMatches = oRE.Execute(s)
For Each objMatch In oMatches
Wscript.Echo objMatch.Submatches(0)
Next
要替换,请使用适当的分组和方法:
oRE.Pattern = "(<ContactPrimaryMobile>).*?(</ContactPrimaryMobile>)"
' and then
s = oRE.Replace(s,"SOME_NEW_VALUE")
我知道您明确说过 regex 并且您已经有了答案,但是实现相同最终目标的另一种方法是改用 XML 解析器。
option explicit
dim xmldoc
set xmldoc = CreateObject("MSXML2.DomDocument")
xmldoc.load "doc.xml"
dim primaryMobileNode
set primaryMobileNode = xmldoc.selectSingleNode("/doc/ContactPrimaryMobile")
primaryMobileNode.text = "new value"
xmldoc.save "changed-doc.xml"
我有这个 xml:
<doc>
<ContactPrimaryEmail></ContactPrimaryEmail>
<ContactAlternateEmail></ContactAlternateEmail>
<ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile>
<ContactAlternateMobile></ContactAlternateMobile>
</doc>
我想在 VBScript 中应用正则表达式来替换 content "+00xxxxxx" 的属性 ContactPrimaryMobile,只需更改数字:
<ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile>
我是 vbscripting 的新手,我在创建对象和应用模式方面的技能有限,所以请你帮我转换这个正则表达式以在 VBScript 中使用它:
(?<=\<ContactPrimaryMobile\>)(.*)(?=\<\/ContactPrimaryMobile)
更新 我明白了:
Object doesn't support this property or method: 'Submatches'
执行时:
Dim oRE, oMatches
Set oRE = New RegExp
oRE.Pattern = "<ContactPrimaryMobile>(.*?)</ContactPrimaryMobile>"
oRE.Global = True
Set oMatches = oRE.Execute("<doc><ContactPrimaryEmail></ContactPrimaryEmail><ContactAlternateEmail></ContactAlternateEmail><ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile><ContactAlternateMobile></ContactAlternateMobile></doc>")
Wscript.Echo oMatches.Submatches(0)
首先,VBScript regex不支持lookbehinds,你需要捕获两个字符串之间的部分。
接下来,您需要通过访问 .Execute
正则表达式匹配后的匹配对象来获取 子匹配 ,并获取其 .Submatches(0)
:
Dim oRE, oMatches, objMatch
oRE.Pattern = "<ContactPrimaryMobile>(.*?)</ContactPrimaryMobile>"
然后
Set oMatches = oRE.Execute(s)
For Each objMatch In oMatches
Wscript.Echo objMatch.Submatches(0)
Next
要替换,请使用适当的分组和方法:
oRE.Pattern = "(<ContactPrimaryMobile>).*?(</ContactPrimaryMobile>)"
' and then
s = oRE.Replace(s,"SOME_NEW_VALUE")
我知道您明确说过 regex 并且您已经有了答案,但是实现相同最终目标的另一种方法是改用 XML 解析器。
option explicit
dim xmldoc
set xmldoc = CreateObject("MSXML2.DomDocument")
xmldoc.load "doc.xml"
dim primaryMobileNode
set primaryMobileNode = xmldoc.selectSingleNode("/doc/ContactPrimaryMobile")
primaryMobileNode.text = "new value"
xmldoc.save "changed-doc.xml"