Tag parsing in python

Tim Chase python.list at tim.thechases.com
Sat Aug 28 13:44:37 EDT 2010


On 08/28/10 11:14, agnibhu wrote:
> For example say I've key words like abc: bcd: cde: like that... So the
> user may use like
> abc: How are you bcd: I'm fine cde: ok
>
> So I've to extract the "How are you" and "I'm fine" and "ok"..and
> assign them to abc:, bcd: and cde: respectively..

For this, you can do something like

 >>> s = "abc: how are you bcd: I'm fine cde: ok"
 >>> import re
 >>> r = re.compile(r'(\w+):\s*((?:[^:](?!\w+:))*)')
 >>> r.findall(s)
[('abc', 'how are you'), ('bcd', "I'm fine"), ('cde', 'ok')]

Yes, it's a bit of a gnarled regexp, but it seems to do the job.

> There may be combination of keyowords introduced in future.
> like abc: xy: How are you So new keywords qualifying the other
> keywords so on.

I'm not sure I understand this bit of what you're asking.  If you 
have

   s = "abc: xy: How are you"

why should that not be parsed as

 >>> r.findall("abc: xy: How are you")
[('abc', ''), ('xy', 'How are you')]

as your initial description prescribes?

-tkc








More information about the Python-list mailing list