如何删除 Python 中的正则表达式子字符串?
How can I delete regex substrings in Python?
如何删除 Python 中以 @
开头并以空白 space 结尾的子字符串?
另外我想删除所有以 http
开头的序列,例如:
Input "ABC @XYZ ABC @Python ABC http://www.whosebug.com ABC"
Output "ABC ABC ABC ABC"
您可以使用正则表达式 sub
方法并替换为空字符串 ''
:
import re
input = 'ABC @XYZ ABC @Python ABC http://www.whosebug.com ABC'
output = re.sub(r'(http|@)\S*\s', '', input)
print output # 'ABC ABC ABC ABC'
如何删除 Python 中以 @
开头并以空白 space 结尾的子字符串?
另外我想删除所有以 http
开头的序列,例如:
Input "ABC @XYZ ABC @Python ABC http://www.whosebug.com ABC"
Output "ABC ABC ABC ABC"
您可以使用正则表达式 sub
方法并替换为空字符串 ''
:
import re
input = 'ABC @XYZ ABC @Python ABC http://www.whosebug.com ABC'
output = re.sub(r'(http|@)\S*\s', '', input)
print output # 'ABC ABC ABC ABC'