replace words

H

hagai26

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"
 
P

Peter Otten

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
 
P

Paul McGuire

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
 
S

Scott David Daniels

Peter said:
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/Cookbook/Python/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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top