468,253 Members | 1,241 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

String replace with return value?

How can I do a "".replace operation which tells me if anything was
actually replaced?

Right now I am doing something like:

if searchTerm in text:
text = text.replace( searchTerm, 'other' )

But to me this seems inefficient since the 'in' operation will search
through the whole string, and then the 'replace' will as well.

The strings are quite large, they're whole web pages. Thanks for any help.

Roose
Jul 18 '05 #1
5 2239
On Mon, Oct 11, 2004 at 09:47:48AM +0000, Roose wrote:
How can I do a "".replace operation which tells me if anything was
actually replaced?
You could compare the strings from before and after the replacement.
Right now I am doing something like:

if searchTerm in text:
text = text.replace( searchTerm, 'other' )

But to me this seems inefficient since the 'in' operation will search
through the whole string, and then the 'replace' will as well.
Yep. Why not just omit the check and just do the replacement?
The strings are quite large, they're whole web pages. Thanks for
any help.


Then I'd suggest to eventually switch to an existing templating
solution for HTML.

My guess is, however, that you're relatively new to Python. And in
this case, reinventing the wheel a few times is a good way to learn
the language and the libraries. We all did that ;-)

For your particular question, you can also use the re module to
perform string substitutions. And then, there's re.subn or the subn
method of regular expression objects, which will return both the
modified string and the number of substitutions.

Here's a short example for the subn method:

#############################################
import re
import time

modifiedRe = re.compile(r"\$modifieddate")

text = "this webpage was last modified on $modifieddate"

print text
text, replacements = modifiedRe.subn(time.asctime(), text)
print text
print replacements, "replacements made"
#############################################

HTH,

-- Gerhard
Jul 18 '05 #2
Roose wrote:
How can I do a "".replace operation which tells me if anything was
actually replaced?

Right now I am doing something like:

if searchTerm in text:
text = text.replace( searchTerm, 'other' )


Try:

newtext = oldtext.replace(searchTerm, 'other')
if newtext is oldtext:
print "nothing changed"
else:
print "modified text"

You can almost certainly get away with using 'is' here since if replace
doesn't replace anything it simply returns the original string (this is an
implementation detail though, I don't know that there is a cast iron
guarantee that a future implementation won't copy the string if it hasn't
changed, although it would be pretty perverse if it did.)
Jul 18 '05 #3
Duncan Booth wrote:
How can I do a "".replace operation which tells me if anything was
actually replaced?
Right now I am doing something like:

if searchTerm in text:
text = text.replace( searchTerm, 'other' )


Try:

newtext = oldtext.replace(searchTerm, 'other')
if newtext is oldtext:
print "nothing changed"
else:
print "modified text"

You can almost certainly get away with using 'is' here since if replace
doesn't replace anything it simply returns the original string


depends on how you define "if anything was actually replaced", of course:
mytext = "hello" mytext is mytext.replace("l", "l") False mytext == mytext.replace("l", "l") True
mytext is mytext.replace("x", "x") True mytext == mytext.replace("x", "x")

True

</F>

Jul 18 '05 #4
Gerhard Haering wrote:
On Mon, Oct 11, 2004 at 09:47:48AM +0000, Roose wrote:
How can I do a "".replace operation which tells me if anything was
actually replaced?

You could compare the strings from before and after the replacement.

Right now I am doing something like:

if searchTerm in text:
text = text.replace( searchTerm, 'other' )

But to me this seems inefficient since the 'in' operation will search
through the whole string, and then the 'replace' will as well.

Yep. Why not just omit the check and just do the replacement?

I thought of that, but don't you see, that's pretty much the same thing.
It's a question of doing an 'in' and a replace vs. a compare and a
replace. Both in and equality comparison are linear operations, so
choosing over the other doesn't really matter. A speedup can still be
obtained by traversing the string once instead of twice.

Now, the person who suggested using 'is' (which is constant time) was
onto something, but alas replace doesn't work like that. As someone
demonstrated, it returns a new string in any case.


The strings are quite large, they're whole web pages. Thanks for
any help.

Then I'd suggest to eventually switch to an existing templating
solution for HTML.


That doesn't apply, since I'm not generating web pages. I'm
transforming existing ones in a spider kind of program.
For your particular question, you can also use the re module to
perform string substitutions. And then, there's re.subn or the subn
method of regular expression objects, which will return both the
modified string and the number of substitutions.


Thank you, that's just what I was looking for.

Roose
Jul 18 '05 #5
Roose wrote:
I thought of that, but don't you see, that's pretty much the same thing.
It's a question of doing an 'in' and a replace vs. a compare and a
replace. Both in and equality comparison are linear operations, so
choosing over the other doesn't really matter. A speedup can still be
obtained by traversing the string once instead of twice.

Now, the person who suggested using 'is' (which is constant time) was
onto something, but alas replace doesn't work like that. As someone
demonstrated, it returns a new string in any case.


No, it only returns a new string if it actually does a replacement.

Frederick Lundh confused the issue by pointing out that a replace can do a
replacement on a string but leave it unchanged. Provided you can guarantee
that the original string and replacement string are different this won't
matter.

To keep Frederick happy, you could change my example to:

if searchTerm == replacement:
newtext = oldtext
else:
newtext = oldtext.replace(searchTerm, replacement)

if newtext is oldtext:
print "nothing changed"
else:
print "modified text"

I will be surprised though if the overhead of comparing strings using '=='
even if they are large web pages if measurable in terms of either server
loading or time to retrieve a page.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Locusta | last post: by
16 posts views Thread by BBM | last post: by
21 posts views Thread by gary | last post: by
5 posts views Thread by int main(void) | last post: by
1 post views Thread by Gene Kelley | last post: by
2 posts views Thread by kevin.eugene08 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.