473,395 Members | 1,623 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.

replace words

What is the way for replacing in a string from . to . the sentence?
for example:
"been .taken. it may be .left. there,
even if the .live coals were not. cleared"
I want to do this-> replace(\.(.*)\.,\.start (1) end\.)
result:
"been .start taken end. it may be .start left end. there,
even if the .start live coals were not end. cleared"

Oct 26 '05 #1
3 1752
ha*****@gmail.com wrote:
What is the way for replacing in a string from . to . the sentence?
for example:
"been .taken. it may be .left. there,
even if the .live coals were not. cleared"
I want to do this-> replace(\.(.*)\.,\.start (1) end\.)
result:
"been .start taken end. it may be .start left end. there,
even if the .start live coals were not end. cleared"


Use \1 to refer to the group in the substitution expression.
You also need to change the regex to non-greedy match (the trailing ?).
Otherwise you only get one big match from .taken ... not.

import re
s = ("been .taken. it may be .left. there, "
"even if the .live coals were not. cleared")

r = re.compile(r"\.(.*?)\.")
print r.sub(r".start \1 end.", s)

Peter

Oct 26 '05 #2

ha*****@gmail.com wrote:
What is the way for replacing in a string from . to . the sentence?
for example:
"been .taken. it may be .left. there,
even if the .live coals were not. cleared"
I want to do this-> replace(\.(.*)\.,\.start (1) end\.)
result:
"been .start taken end. it may be .start left end. there,
even if the .start live coals were not end. cleared"


Here is a pyparsing version. Yes, it is more verbose, and, being pure
Python, is not as fast as regexp. But the syntax is very easy to
follow and maintain, and the parse actions can add quite a bit of
additional logic during the parsing process (for example, rejecting dot
pairs that span line breaks).

-- Paul
from pyparsing import Literal,SkipTo

data = """been .taken. it may be .left. there,
even if the .live coals were not. cleared"""

DOT = Literal(".")

# expression to match, comparable to regexp r"\.(.*?)\."
dottedText = DOT + SkipTo(DOT).setResultsName("body") + DOT

# action to run when match is found - return modified text
def enhanceDots( st, loc, tokens):
return ".start " + tokens.body + " end."
dottedText.setParseAction( enhanceDots )

# transform the string
print dottedText.transformString( data )
Gives:
been .start taken end. it may be .start left end. there,
even if the .start live coals were not end. cleared

Oct 26 '05 #3
Peter Otten wrote:
ha*****@gmail.com wrote:

What is the way for replacing in a string from . to . the sentence?
for example:
"been .taken. it may be .left. there,
even if the .live coals were not. cleared"
I want to do this-> replace(\.(.*)\.,\.start (1) end\.)
result:
"been .start taken end. it may be .start left end. there,
even if the .start live coals were not end. cleared"

Use \1 to refer to the group in the substitution expression.
You also need to change the regex to non-greedy match (the trailing ?).
Otherwise you only get one big match from .taken ... not.

import re
s = ("been .taken. it may be .left. there, "
"even if the .live coals were not. cleared")

r = re.compile(r"\.(.*?)\.")
print r.sub(r".start \1 end.", s)

Peter

Perhaps you can use a variant of:
http://aspn.activestate.com/ASPN/Coo.../Recipe/231347

def wrap(source, pre, post):
gen = iter(source)
for portion in gen:
yield portion
try:
bracketed = gen.next()
except StopIteration:
break
yield pre
yield bracketed
yield post

def mangledots(string):
return ''.join(wrap(string.split('.'), '.start ', ' end.'))

print mangledots('been .taken. it may be .left. there, '
'even if the .live coals were not. cleared')

--Scott David Daniels
sc***********@acm.org
Oct 28 '05 #4

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

Similar topics

5
by: susan0nz | last post by:
I have some strings like this: "The temperature is 18 Celsius today". Some strings may have more than one value in them, like "The temperature is 18 Celsius today. Yesterday is was 17 Celsius". ...
2
by: Leif K-Brooks | last post by:
For my web-based application, I need to make twelve different calls to str.replace for the content of /every/ page on /every/ page view (or find a harder to implement and probably less elegant...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
4
by: Prasad S | last post by:
Hello I wish to replace all the characters in a string except those which are inside '<' & '>' characters. And there could be multiple occurences of < & > within the string. e.g. string =...
16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
3
by: gregpinero | last post by:
I'm trying to write a little script that will have a list of word pairs which will loop through that list and replace all instances of each word with the other word. I'm very new to javascript...
2
by: Raed Sawalha | last post by:
I have the following text:- Brian went to stadium to watch the soccer game, Brian MacWoods is bussiness man and very rich man. Brian likes to run every morning on beachside. the problem i...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
1
by: Curious | last post by:
I'm working on a word replacement program in .NET. I have a list of words spelt in American English and need to replace them with translated British spelling while keeping the upper or lower cases...
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.