473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

multiline regular expression (replace)

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
May 29 '07 #1
6 3775
On May 29, 2:03 am, Zdenek Maxa <zdenekm...@yahoo.co.ukwrote:
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

May 29 '07 #2
ha**********@gmail.com wrote:
On May 29, 2:03 am, Zdenek Maxa <zdenekm...@yahoo.co.ukwrote:
>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
May 29 '07 #3
Zdenek Maxa wrote:
ha**********@gmail.com wrote:
>On May 29, 2:03 am, Zdenek Maxa <zdenekm...@yahoo.co.ukwrote:
>>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 ----------------

May 29 '07 #4
On May 29, 11:03 am, Zdenek Maxa <zdenekm...@yahoo.co.ukwrote:
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)
If it helps, I have the following function:

8<-----------------------------------------------------------
def update_xml(infile, outfile, mapping, deep=False):
from xml.etree import cElementTree as ET
from utils.elementfilter import ElementFilter
doc = ET.parse(infile)
efilter = ElementFilter(doc.getroot())
changes = 0
for key, val in mapping.iteritems():
pattern, repl = val
efilter.filter = key
changes += efilter.sub(pattern, repl, deep=deep)
doc.write(outfile, encoding='UTF-8')
return changes

mapping = {
'/portal/content-node[@type=="page"]/@action': ('.*', 'ZZZZ'),
'/portal/web-app/portlet-app/portlet/localedata/title':
('Portal', 'Gateway'),
}

changes = update_xml('c:\\working\\tmp\\test.xml', 'c:\\working\\tmp\
\test2.xml', mapping, True)

print 'There were %s changes' % changes
8<-----------------------------------------------------------

where utils.elementfilter is this module:

http://gflanagan.net/site/python/ele...ementfilter.py

It doesn't support `re` flags, but you could change the sub method of
elementfilter.ElementFilter to do so, eg.(UNTESTED!):

def sub(self, pattern, repl, count=0, deep=False, flags=None):
changes = 0
if flags:
pattern = re.compile(pattern, flags)
for elem in self.filtered:
...
[rest of method unchanged]
...

Gerard

May 29 '07 #5
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:
ha**********@gmail.com wrote:
>On May 29, 2:03 am, Zdenek Maxa <zdenekm...@yahoo.co.ukwrote:
>>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
May 29 '07 #6
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:

>ha**********@gmail.com wrote:
>>On May 29, 2:03 am, Zdenek Maxa <zdenekm...@yahoo.co.ukwrote:
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

May 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: David Elliott | last post by:
I have an expression that works for single line but not multiline. What am I missing? expression = "<div(?<data1>.*?)>(?<data2>.*?)</div>"; MatchCollection mc = Regex.Matches(data, expression,...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
1
by: Laser Lu | last post by:
Hi, all, I'm now writing a program to compress JavaScript code. One puzzle is how to write a regular expression to find out and remove all the redundent blank spaces. However, those blank spaces...
3
by: Mark | last post by:
To validate the length of a multiline textbox, I'm told that I have to use a regular expression validator. The regular expression below limits it to 25 characters in length, but if the user enters...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
5
by: Grzegorz Danowski | last post by:
Hi, I'd like to read all lines of caption text from a string: .... Name ="Paragraph" Caption ="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. " "The...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.