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

replace words

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.