Help with Python regex
#1
Hi, I'm trying to read and replace lines in an xml file.

I'm trying to find "<control type="videowindow">" and it's companion "</control>"
replacing "<control type="videowindow">" is easy enough (since there is only one), but finding it's matching "</control>" is the tricky part.

I figure the easiest way would be to readline the xml file and use search n replace, but I can't figure out the correct regex pattern to match the string.

Could some either suggest a better alternative or assist me in creating a regex for this pattern?

Thanks
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#2
You probably can use ElementTree to parse then write the XML file.
Reply
#3
(2014-02-23, 07:59)giftie Wrote: You probably can use ElementTree to parse then write the XML file.

Yeah I was thinking about ET or mini Dom, but the xml has many entries. I need to maintain the format while only changing the one control. If I use a parser wouldn't I lose track of its line position?

Regex is the biggest pain to figure out.
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#4
I ended up adding a unique comment to the end of "</control> <!-- end -->"
So search and replace has a string it can match...
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#5
regexes are a wrong tool for the task. Look at BeautifulSoup, for example.
Reply
#6
(2014-02-23, 21:14)Roman_V_M Wrote: regexes are a wrong tool for any task.

FYP
Reply
#7
Yeah I'd second using something like lxml or ElementTree.
Reply
#8
thanks for the alternatives, I'll test them out.
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply

Logout Mark Read Team Forum Stats Members Help
Help with Python regex0