Removing a substring from string

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 3
Reputation: wrobl1rt is an unknown quantity at this point 
Solved Threads: 0
wrobl1rt wrobl1rt is offline Offline
Newbie Poster

Removing a substring from string

 
0
  #1
Nov 2nd, 2009
I am trying to remove a specific substring from a string... Here are the doctests. I'm just absolutely stumped. Can someone point me in the right direction on where to start?

def remove(sub, s):

"""
>>> remove('an', 'banana')
'bana'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


def remove_all(sub, s):
"""
>>> remove('an', 'banana')
'ba'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


if __name__ == '__main__':
import doctest
doctest.testmod()
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #2
Nov 2nd, 2009
A possible implementation of remove_all
  1. import re
  2.  
  3. def remove_all(sub, s):
  4. """
  5. >>> remove_all('an', 'banana')
  6. 'ba'
  7. >>> remove_all('cyc', 'bicycle')
  8. 'bile'
  9. >>> remove_all('iss', 'Mississippi')
  10. 'Mippi'
  11. """
  12.  
  13. return re.sub(re.escape(sub), '', s)
  14.  
  15.  
  16. if __name__ == '__main__':
  17. import doctest
  18. doctest.testmod()
Note that a regex is compiled on each call.
Last edited by Gribouillis; Nov 2nd, 2009 at 5:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: wrobl1rt is an unknown quantity at this point 
Solved Threads: 0
wrobl1rt wrobl1rt is offline Offline
Newbie Poster
 
0
  #3
Nov 2nd, 2009
Thank you!! would you mind explaining why the implementation of return re.sub(re.escape(sub), '', s) works for the remove_all function specifically?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #4
Nov 2nd, 2009
In fact, there is a simpler method
  1. def remove_all(sub, s):
  2. """
  3. >>> remove_all('an', 'banana')
  4. 'ba'
  5. >>> remove_all('cyc', 'bicycle')
  6. 'bile'
  7. >>> remove_all('iss', 'Mississippi')
  8. 'Mippi'
  9. """
  10. return s.replace(sub, '')
  11.  
  12. if __name__ == '__main__':
  13. import doctest
  14. doctest.testmod()
I don't understand your question, re.sub replaces every occurrence of a pattern by a given string, that's how it works.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: wrobl1rt is an unknown quantity at this point 
Solved Threads: 0
wrobl1rt wrobl1rt is offline Offline
Newbie Poster
 
0
  #5
Nov 2nd, 2009
oh ok... the book I am going by gave me these problems when I haven't learned about re.sub yet. Ive looked back and it was not even mentioned.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 60
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #6
Nov 3rd, 2009
you could also try this:

  1. def remove( s, rem ):
  2. newS = ""
  3. i = 0
  4. while i < len( s ):
  5. if s[ i : i + len( rem ) ] == rem:
  6. i += len( rem )
  7. else:
  8. newS += s[ i ]
  9. i += 1
  10. print newS
  11.  
  12. s = "Mississippi"; r = "iss"
  13. remove( s, r )

the old facion way
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #7
Nov 3rd, 2009
If
remove('an', 'banana') --> 'bana'
then this should be
remove('iss', 'Mississippi') --> 'Missippi'
So you can use ...
  1. def remove(sub, s):
  2. # replace first sub with empty string
  3. return s.replace(sub, "", 1)
  4.  
  5. # test
  6. print( remove('an', 'banana') ) # --> bana
  7. print( remove('iss', 'Mississippi') ) # --> Missippi
If you want to remove all subs, then use -1 in replace() which actually is the default value ...
  1. def remove_all(sub, s):
  2. # replace all sub with empty string
  3. return s.replace(sub, "", -1)
  4.  
  5. # test
  6. print( remove_all('an', 'banana') ) # --> ba
  7. print( remove_all('iss', 'Mississippi') ) # --> Mippi
Python comes with a wealth of thoroughly tested string functions, might as well use them.
Last edited by vegaseat; Nov 3rd, 2009 at 10:27 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC