473,395 Members | 1,790 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,395 software developers and data experts.

Regular Expression for pattern substitution

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
3 1518
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
"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
Hrm, thought it had one. Guess it would help if I actually used regular
expression for replacement.

Jul 19 '05 #4

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...
5
by: Karin Jensen | last post by:
Hi I am writing in PHP and trying to work with regular expressions on records in a multilanguage database. I understand regexp basics, but have bitten off more than I can chew here and really...
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...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
3
by: shapper | last post by:
Hello, I have a regular expression to validate email addresses: "\w+(\w+)*@\w+(\w+)*\.\w+(\w+)*" Now I need to force all emails to be from a given domain, for example, accept only:...
6
by: rorymo | last post by:
I have a regular expression that allows only certain characters to be valid in an xml doc as follows: <xs:pattern value="^*" /> What I want to do is also allow any unicode character that is...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
5
by: antar2 | last post by:
Hello, I am a beginner in Python and am not able to use a list element for regular expression, substitutions. list1 = list2 = Suppose that I want to substitute the vowels from list2 that...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.