regexp substitution - a lot of work!

Eddie Corns eddie at holyrood.ed.ac.uk
Wed Jun 16 17:12:27 CEST 2004


Lukas Holcik <xholcik1 at fi.muni.cz> writes:

> 	Hi Python crazies!:))
>There is a problem to be solved. I have a text and I have to parse 
>it using a lot of regular expressions. In (lin)u(ni)x I could write in 
>bash:

> 	cat file | sed 's/../../' | sed 's/../../' .. .. .. > parsed_file

In Unix you would actually do:

  $ sed 's/pat1/rep1/ s/pat2/rep2/ ...' <infile >outfile

to do the replacements in one pass.  (you will now anyway :)

>I write a parser in python and what I must do is:

> 	regexp = re.compile(..)
> 	result = regexp.search(data)
> 	while result:
> 		data = data[:result.start()] + .. result.group(..) + \
> 			data[result.end():]
> 		result = regexp.search(data)

> 	... for one regexp substitution

> 	instead of just: s/../../


>That is quite a lot of work! Don't you know some better and easier way? 
>Thanks in advance,

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330

Eddie



More information about the Python-list mailing list