How to replace multiple-line text

Martin v. Loewis martin at v.loewis.de
Thu Jul 11 03:00:27 EDT 2002


David Lees <debl2nonospammyyy at bellatlantic.net> writes:

> I would like to process a code file and substitute one multiline block
> of code for another.  I know how to do this in Python by brute force
> scanning through on a line by line basis until the first line of the
> pattern is found then looping over the rest of the lines in the target
> pattern for a match, then substituting in an output string multiline
> substitution.  But I am sure there is a neater solution, perhaps using
> regular expressions.  Could someone point me towards sample code or
> something similar that I could modify.

Regular expressions might work, as might plain list slicing. You will
need to describe the properties of this replace process in more detail
to get good advise. In plain English, please describe:
- how would you recognize (by visual inspection) that the multiline
  block starts?
- how do you recognize that it ends?
- how does the block you want to insert depend on the block you want
  to remove?

Regards,
Martin



More information about the Python-list mailing list