By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

Regular Expression for pattern substitution

P: n/a
It'd be silly to write the code for it if it already
exists somewhere in the Python re or sre library
module:

I need to find and replace all strings in a text file
from a certain pattern to another pattern.

so for example if I see 'this(\D*)that' anywhere in
the file then I'd like to make is 'that(\D*)this'
where the middle part of the strings remains
unmodified.

Any suggestions?

Peace.
Vibha

PS. How do I avoid getting my email ID web-published
for this mailing list.?

__________________________________________________ __
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports.yahoo.com
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
re.replace.

I don't think there's any way to avoid it. Except maybe having an alias
email address or a fake one.

Jul 19 '05 #2

P: n/a
"Deval L" wrote:
re.replace.
There isn't a re.replace; be careful when you reply to newbies.
"Vibha Tripathi" wrote:
It'd be silly to write the code for it if it already
exists somewhere in the Python re or sre library
module:

I need to find and replace all strings in a text file
from a certain pattern to another pattern.

so for example if I see 'this(\D*)that' anywhere in
the file then I'd like to make is 'that(\D*)this'
where the middle part of the strings remains
unmodified.

Any suggestions?

Peace.
Vibha
Fire up the interpreter and write:
import re
line = 'see this man with that woman holding this dog and that cat'
r = re.compile(r'this(\D*?)that')
r.sub(r'that\1this',line)

'see that man with this woman holding that dog and this cat'
George

Jul 19 '05 #3

P: n/a
Hrm, thought it had one. Guess it would help if I actually used regular
expression for replacement.

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.