2

I have this xml:

<doc>
<ContactPrimaryEmail></ContactPrimaryEmail>
<ContactAlternateEmail></ContactAlternateEmail> 
<ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile>
<ContactAlternateMobile></ContactAlternateMobile> 
</doc>

I want to apply a regular expression in VBScript to replace the content "+00xxxxxx" of the attribute ContactPrimaryMobile, simply change the number:

<ContactPrimaryMobile>+00xxxxxx</ContactPrimaryMobile>

I am new to vbscripting and my skills in creating the objects and applying the pattern are limited, so please can you help me converting this regex to use it in VBScript:

(?<=\<ContactPrimaryMobile\>)(.*)(?=\<\/ContactPrimaryMobile)

UPDATE I get this:

Object doesn't support this property or method: 'Submatches'

when executing:

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)
5
  • Use <ContactPrimaryMobile>(.*?)</ContactPrimaryMobile> and grab match.Submatches(0) value. Show your code if you need more concrete help. Commented Sep 14, 2018 at 9:10
  • I get this Object doesn't support this property or method: 'Submatches' when executing: 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) Any further help? Commented Sep 14, 2018 at 9:28
  • Please add that to the question body. Why are you trying to get Submatches on a match collection? Access the first item. Wscript.Echo oMatches(0).Submatches(0) Commented Sep 14, 2018 at 9:28
  • Thanks a lot for your reply! Commented Sep 14, 2018 at 9:30
  • No just one, but what I actually need is to replace the number in the xml with a new one. Commented Sep 14, 2018 at 9:38

2 Answers 2

6

First of all, VBScript regex does not support lookbehinds, you need to capture the part in between the two strings.

Next, you need to obtain the submatch by accessing the match object after you .Execute the regex match, and get its .Submatches(0):

Dim oRE, oMatches, objMatch
oRE.Pattern = "<ContactPrimaryMobile>(.*?)</ContactPrimaryMobile>"

and then

Set oMatches = oRE.Execute(s)
For Each objMatch In oMatches
  Wscript.Echo objMatch.Submatches(0)
Next

To replace, use the appropriate groupings and method:

oRE.Pattern = "(<ContactPrimaryMobile>).*?(</ContactPrimaryMobile>)"
' and then
s = oRE.Replace(s,"$1SOME_NEW_VALUE$2")
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, I get it! Now, how can I update the number in the string?
2

I know you explicitly said and you have your answer but an alternative approach to getting the same end goal is to use an XML parser instead.

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"

1 Comment

Thanks for your reply. good point, I thought it too. But I don't read the xml from from a file. It is coming from a database as an adodb object. I thought it would be faster to use regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.