multiline regular expression (replace)

Steve Holden steve at holdenweb.com
Tue May 29 08:36:00 EDT 2007


Zdenek Maxa wrote:
> half.italian at gmail.com wrote:
>> On May 29, 2:03 am, Zdenek Maxa <zdenekm... at yahoo.co.uk> wrote:
>>   
>>> Hi all,
>>>
>>> I would like to perform regular expression replace (e.g. removing
>>> everything from within tags in a XML file) with multiple-line pattern.
>>> How can I do this?
>>>
>>> where = open("filename").read()
>>> multilinePattern = "^<tag> .... <\/tag>$"
>>> re.search(multilinePattern, where, re.MULTILINE)
>>>
>>> Thanks greatly,
>>> Zdenek
>>>     
>> Why not use an xml package for working with xml files?  I'm sure
>> they'll handle your multiline tags.
>>
>> http://effbot.org/zone/element-index.htm
>> http://codespeak.net/lxml/
>>
>> ~Sean
>>
>>   
> 
> Hi,
> 
> that was merely an example of what I would like to achieve. However, in 
> general, is there a way for handling multiline regular expressions in 
> Python, using presumably only modules from distribution like re?
> 
> Thanks,
> Zdenek

So you mean you don't know how to *create* multiline patterns?

One way is to use """ ... """ or ''' ... ''' quoting, which allows you 
to include newlines as part of your strings. Another is to use \n in 
your strings to represent newlines.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com        squidoo.com/pythonology
tagged items:         del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------




More information about the Python-list mailing list