[Tutor] Optional groups in RE's
Mark Tolonen
metolone+gmane at gmail.com
Sun Apr 12 02:50:44 CEST 2009
"Moos Heintzen" <iwasroot at gmail.com> wrote in message
news:7b13ba330904111546n21d90202i7572a75c55b02a23 at mail.gmail.com...
> Hello Tutors!
>
> I was trying to make some groups optional in a regular expression, but
> I couldn't do it.
>
> For example, I have the string:
>
>>>> data = "<price>42</price> sdlfks d f<ship>60</ship> sdf sdf
>>>> <title>Title</title>"
[ re attempts stripped]
Your data looks like XML. If it is actually well-formed XML, have you tried
ElementTree?
PythonWin 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for
further copyright information.
>>> from xml.etree import ElementTree as ET
>>> data = "<object><price>42</price> sdlfks d f<ship>60</ship> sdf sdf
>>> <title>Title</title></object>"
>>> t=ET.fromstring(data)
>>> t.tag
'object'
>>> for e in t.getchildren():
... print e.tag,e.text
...
price 42
ship 60
title Title
>>>
-Mark
More information about the Tutor
mailing list