1

How can I delete a substring which starts with @ and ends with a blank space in Python?
Also I want to remove all the sequences starting with http, e.g.:

Input  "ABC @XYZ ABC @Python ABC http://www.stackoverflow.com ABC"
Output "ABC ABC ABC ABC"
3
  • Have you heard about regular expressions? docs.python.org/2/library/re.html Commented May 18, 2015 at 17:44
  • 1
    @freakish I think he did, since the title mentions them. Commented May 18, 2015 at 17:45
  • Oh, missed that. Then the next question is: what have you tried? Commented May 18, 2015 at 17:45

1 Answer 1

4

You can use the regex sub method and replace with the empty string '':

import re
input = 'ABC @XYZ ABC @Python ABC http://www.stackoverflow.com ABC'
output = re.sub(r'(http|@)\S*\s', '', input) 
print output  # 'ABC ABC ABC ABC'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.