Skip to content Skip to sidebar Skip to footer

Issues With Python 3.x Multiline Regex?

I am very new to python and started coding with it a few weeks ago. Since now I was able to resolv any issues with researching and reading. But this issue gives me now headaches si

Solution 1:

In short: don't. If you're in the state of learning Python (or any other language, for that matter), trying to analyze XML nodes with regular expression is usually considered an anti-pattern. Instead, use a parser (that's what they were made for).


For your specific example this might come down to:
from lxml import etree
tree = etree.parse('test.xml')
root = tree.getroot()

for title in root.xpath("//item/title"):
    print(title.text)

And yields

It's Bugtober, with Adobe Flash crashes, numerous CVE vulnerability patches for Wi-Fi and routers, and an Intel SPI vulnerability patch for most Xeon D Supermicro SuperServers
Supermicro Xeon D SuperServer BIOS 1.2c / IPMI 3.58 released
Windows 10 Fall Creators Update introduces GPU monitoring features built right into Task Manager
VMUG Advantage EVALExperience includes latest VMware vRealize Log Insight 4.5 syslog server appliance for easy vSphere, vSAN, IoT, and networking gear log file analysis
Road-warrior productivity boosted by ASUS ZenScreen MB16AC secondary travel display that connects to Mac or PC with just one USB-C or USB 3.0 cable


You see, this makes for cleaner and better to understand code. You might need to install lxml via pip install lxml first.
Note: there was an error in your XML file which I needed to correct for this to work (the link tag was opened but never closed).

Post a Comment for "Issues With Python 3.x Multiline Regex?"