2

I have 3 xml tag with the same tag name in 1 string :

<Name>Case1</Name> <Name>Case2</Name><Name>Case3</Name>

I want to extract the content of each node using regular expression. Here is the regexp i have tried so far

(<Name>)(.*)(<\/Name>)

But it doesn't match as expected. It matches the whole string <Name>Case1</Name> <Name>Case2</Name><Name>Case3</Name>

Any help would be appreciate.

Anh Nguyen
  • 313
  • 5
  • 11
  • 22
  • Yes ... the [greedy trap](http://www.rexegg.com/regex-quantifiers.html#greedytrap). Consider parsing these values out instead. – hwnd Nov 13 '14 at 10:18

4 Answers4

4

RegExps match greedy (the longest possible sequence) per default. Use ".*?" to specify non-greedy matches:

>> set r = New RegExp
>> r.Pattern = "<Name>(.*?)</Name>"
>> s = "<Name>Case1</Name> <Name>Case2</Name><Name>Case3</Name>"
>> WScript.Echo r.Execute(s)(0).Submatches(0)
>>
Case1
>>

Please consider to use XML tools (XPath, ...) to work with XML data.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
2

I try this Regex.

<name>((?:(?!</?name[ >]).)*)</name>

SEE DEMO:http://regex101.com/r/kP8uF5/22

depsai
  • 405
  • 2
  • 14
1

Dont use RegEx to parse XML/HTML. Use an XML Parser instead.

I encourage the use of XPath (http://de.wikipedia.org/wiki/XPath)

More info why RegEx is no XML Parser can be found here: RegEx match open tags except XHTML self-contained tags

Community
  • 1
  • 1
Fabian S.
  • 2,441
  • 2
  • 24
  • 34
0

Your regular expression doesn't work as you expect, because you're using a greedy match (.*) instead of a non-greedy match (.*?).

However, you shouldn't be using regular expressions for parsing XML at all. Use an actual XML parser for extracting data from XML data structures.

data = "<root><Name>Case1</Name> <Name>Case2</Name><Name>Case3</Name></root>"

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.loadXML data

If xml.parseError Then
  WScript.Echo xml.parseError.reason
  WScript.Quit 1
End If

For Each n In xml.SelectNodes("//Name")
  WScript.Echo n.text
Next
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328