multiline regular expression (replace)

Zdenek Maxa zdenekmaxa at yahoo.co.uk
Wed May 30 03:50:33 EDT 2007


Hi,

Thanks a lot for useful hints to all of you who replied to my question. 
I could easily do now what I wanted.

Cheers,
Zdenek


Holger Berger wrote:
> Hi,
>
> yes:
>
> import re
>
> a="""
> I Am
> Multiline
> but short anyhow"""
>
> b="(I[\s\S]*line)"
>
> print re.search(b, a,re.MULTILINE).group(1)
>
>
> gives
>
> I Am
> Multiline
>
> Be aware that . matches NO newlines!!!
> May be this caused your problems?
>
> regards
>         Holger
>
>
> 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
>>     
>
>   




More information about the Python-list mailing list