Regex Question

Istvan Albert ialbert at mailblocks.com
Wed Aug 20 12:50:07 EDT 2003


Thomas Güttler wrote:
> Hi!
> 
> I want to parse a file that looks like this:
> 
> a0001: basbasdb
> asbddsb
> asdbasdb
> abasbd
> a0002: fffff
> ffff
> ffffff
> ffff

> I think there is a better solution than this, don't you?
It is not clear from what you are saying what are the limitations
for possible key and values. If the ":" character cannot appear
in a key or a value you could do something a lot simpler:

elems = line.split(":")
if len(elems)==2:
	key = elems[0]
	val = elems[1].strip()
else:
	val = elems[0].strip()

If you need to check/enforce a certain key format then
the regexp solution is better.

Istvan.






More information about the Python-list mailing list