Regex Question

Peter Hansen peter at engcorp.com
Wed Aug 20 11:32:53 EDT 2003


Thomas Güttler wrote:
> 
> I want to parse a file that looks like this:
> 
> a0001: basbasdb
> asbddsb
> asdbasdb
> abasbd
> a0002: fffff
> ffff
> ffffff
> ffff
> 
> a???? should be the key and the following lines should
> be the value of a dictionary.
> 
> My solution looks like this:
> 
> regex=re.compile(r'(?:^|\n)(\w\d\d\d\d):((?:.(?!\n\w\d\d\d\d:))*)',
> re.DOTALL)
> matches=regex.findall(content)
> for match in matches:
>     print match
> 
> I think there is a better solution than this, don't you?

It depends.  Does this solution work, and run acceptably quickly?
If so, there is no better solution.  ;-)

(To put it another way: define "better".)

-peter




More information about the Python-list mailing list