RegEx Text Between XML Tags

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Posts: 1391
Joined: 19 Dec 2013, 11:16
Location: USA

RegEx Text Between XML Tags

19 Dec 2013, 11:30

How can I use RegEx to assign a variable to the text between the opening and closing XML tags? For example, how can I get the text between <units> & </units>?


I have tried searching various forums and found the code below which seems to work, but I'm not exactly sure how it works or if it would be the best/accurate way to complete the task:

Code: Select all

FileDelete, Wx.dat
UrlDownloadToFile,, Wx.dat
FileRead, xmldata, Wx.dat

RegExMatch(xmldata, "s)<units>(.*)</units>", m)
MsgBox, %m1%
What I plan to do is assign a variable to each and every item from the XML file, which sometimes has tags using the same name. Figuring out how to work with that will be another challenge.
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: RegEx Text Between XML Tags

19 Dec 2013, 12:54

This handles a lot of the repeating tags.

Code: Select all

xml := URL_ToVar("")
while p := RegExMatch(xml, "Us)<day number=""(\d)"">(.+)</day>", m, p?p+1:1) {	;Search "<day number="x">...</day number="x">
	RegExMatch(m2, "s)<daytime>.+</daytime>", d)								;<daytime>...</daytime>
		day%m1%_daytime := d
	RegExMatch(m2, "s)<nighttime>.+</nighttime>", n)							;<nighttime>...</nighttime>
		day%m1%_nighttime := n
Loop, 5 {
	i := A_Index, p :=0
	while p := RegExMatch(day%i%_daytime, "Us)<([^/]+)>", m, p?p+1:1) 			;<tags>
		if RegExMatch(day%i%_daytime, "U)<.+>\K.+(?=</" m1 ">)", s, p)			;<tag>...</tag>
			Day%i%_daytime_%m1% := s
	p :=0
	while p := RegExMatch(day%i%_nighttime, "Us)<([^/]+)>", m, p?p+1:1)			;<tags>
		if RegExMatch(day%i%_nighttime, "U)<.+>\K.+(?=</" m1 ">)", s, p)		;<tag>...</tag>
			Day%i%_nighttime_%m1% := s

URL_ToVar(URL) {  
    WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    WebRequest.Open("GET", URL)
    return, WebRequest.ResponseText

Edit: See these two links to learn more about how RegEx works: RegExMatch and RegEx Quick Reference. If you have any questions don't hesitate to ask.
User avatar
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: RegEx Text Between XML Tags

21 Dec 2013, 15:57

Code: Select all

xmldata =
doc := loadXML(xmldata)
nodes := doc.selectSingleNode("/units")

MsgBox % nodes.text

for node in nodes.childNodes
	t .= node.NodeName " :`t" node.text "`n"
MsgBox %t%

loadXML(ByRef data)
	o := ComObjCreate("MSXML2.DOMDocument.6.0")
	o.async := false
	return o
See COM Object Reference: MSXML2.DOMDocument.6.0 for more info.
User avatar
Posts: 1292
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark

Re: RegEx Text Between XML Tags

21 Dec 2013, 18:31 post ... ntry625747

Hey if you use the XML Com object you don't need to but it all in to varibles as the object wil hold them all you just need to know more about how to get them out

There is more then one way to do that to...

Return to “Ask For Help”

Who is online

Users browsing this forum: dentopolis, Xtra and 62 guests