WoooHaaaa
WoooHaaaa

Reputation: 20450

Python regex find two groups

>>> text = '<a data-lecture-id="47"\n   data-modal-iframe="https://class.coursera.org/neuralnets-2012-001/lecture/view?lecture_id=47"\n   href="https://class.coursera.org/neuralnets-2012-001/lecture/47"\n   data-modal=".course-modal-frame"\n   rel="lecture-link"\n   class="lecture-link">\nAnother diversion: The softmax output function [7 min]</a>'

>>> import re
>>> re.findall(r'data-lecture-id="(\d+)"|(.*)</a>',a)
>>> [('47', ''), ('', 'Another diversion: The softmax output function [7 min]')]

How do i extract the data out like this:

>>> ['47', 'Another diversion: The softmax output function [7 min]']

I think there should be some smarter regex expressions.

Upvotes: 1

Views: 130

Answers (3)

Serdalis
Serdalis

Reputation: 10489

you use itertools

import re
from itertools import chain, ifilter

raw_found = re.findall(r'data-lecture-id="(\d+)"|(.*)</a>', text)

# simple
found = [x for x in chain(*raw_found) if x]

# or faster
found = [x for x in ifilter(None, chain(*raw_found))]

# or more compact, also just as fast
found = list(ifilter(None, chain(*raw_found)))

print found

Output:

['47', 'Another diversion: The softmax output function [7 min]']

Upvotes: 2

A. Rodas
A. Rodas

Reputation: 20679

It is not recommended to parse HTML with reguar expressions. You can give a try to the xml.dom.minidom module:

from xml.dom.minidom import parseString

xml = parseString('<a data-lecture-id="47"\n   data-modal-iframe="https://class.coursera.org/neuralnets-2012-001/lecture/view?lecture_id=47"\n   href="https://class.coursera.org/neuralnets-2012-001/lecture/47"\n   data-modal=".course-modal-frame"\n   rel="lecture-link"\n   class="lecture-link">\nAnother diversion: The softmax output function [7 min]</a>')
anchor = xml.getElementsByTagName("a")[0]
print anchor.getAttribute("data-lecture-id"), anchor.childNodes[0].data

Upvotes: 2

WoooHaaaa
WoooHaaaa

Reputation: 20450

I find a solution myself:

>>> re.findall('r'data-lecture-id="(\d+)"[\s\S]+>([\s\S]+)</a>',a)
>>> [('47', '\nAnother diversion: The softmax output function [7 min]')]

Looks better, but still have to iterate it to extract a simple list...

Upvotes: 0

Related Questions