Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex doesn't work as expected

Tags:

python

regex

rss

i've crafted this regular expression:

<entry>\\n<(\w+)>(.+?)</\w+>\\n</entry>

to parse the following RSS Feed:

<?xml version="1.0" encoding="UTF-8"?>\n<feed version="0.3" xmlns="http://purl.org/atom/ns#">\n<title>Gmail - Inbox for [email protected]</title>\n<tagline>New messages in your Gmail Inbox</tagline>\n<fullcount>2</fullcount>\n<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />\n<modified>2011-03-15T11:07:48Z</modified>\n<entry>\n<title>con due mail...</title>\n<summary>Gianluca Bargelli http://about.me/proudlygeek/bio</summary>\n<link rel="alternate" href="http://mail.google.com/[email protected]&amp;message_id=12eb9332c2c1fa27&amp;view=conv&amp;extsrc=atom" type="text/html" />\n<modified>2011-03-15T11:07:42Z</modified>\n<issued>2011-03-15T11:07:42Z</issued>\n<id>tag:gmail.google.com,2004:1363345158434847271</id>\n<author>\n<name>me</name>\n<email>[email protected]</email>\n</author>\n</entry>\n<entry>\n<title>test nuova mail</title>\n<summary>Gianluca Bargelli sono tornato!?& http://about.me/proudlygeek/bio</summary>\n<link rel="alternate" href="http://mail.google.com/[email protected]&amp;message_id=12eb93140d9f7627&amp;view=conv&amp;extsrc=atom" type="text/html" />\n<modified>2011-03-15T11:05:36Z</modified>\n<issued>2011-03-15T11:05:36Z</issued>\n<id>tag:gmail.google.com,2004:1363345026546890279</id>\n<author>\n<name>me</name>\n<email>[email protected]</email>\n</author>\n</entry>\n</feed>\n'skinner.com/products/spl].

The problem is that i am not getting any matches by using Python's re module:

import re

regex = re.compile("""<entry>\\n<(\w+)>(.+?)</\w+>\\n</entry>""")
regex.findall(rss_string) # Returns an empty list

Using an online regex tester (such as this) works as expected, so i don't think is a regex problem.

Edit

I am well aware that using regular expressions to parse a Context-Free Grammar is BAD, but in my case the regular expression is likely to work only for that RSS feed (it is a Gmail inbox feed, by the way) and i know i can use an external library/xml parser for this task: it is only an exercise, not an habit.

The question should be Why the following regular expression don't work as expected in Python?

like image 886
Gianluca Bargelli Avatar asked Aug 06 '26 23:08

Gianluca Bargelli


1 Answers

Before the regex compiler sees a string, Python has already processed the slash-escapes, therefore you'd have to escape it twice (e.g. \\\\n for \\n). However, Python has a handy notation for exactly this sort of thing, just stick an r before the string:

regex = re.compile(r"""<entry>\\n<(\w+)>(.+?)</\w+>\\n</entry>""")

By the way, I agree with the others here, do not use regexes to parse XML. However, hopefully you will find this string notation helpful in future regular expressions.

like image 84
dappawit Avatar answered Aug 08 '26 12:08

dappawit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!