假设我有一个字符串
This is a good doll http://www.google.com/a/bs/jdd/etc/etc/a.py
我想得到这样的东西
This is a good doll www.google.com
我在 python 中尝试print re.sub(r'(http://|https://)',"",a))
了函数,但我只能从中删除http://
部分。关于如何在 python 2.7 中实现这一点的任何想法
假设我有一个字符串
This is a good doll http://www.google.com/a/bs/jdd/etc/etc/a.py
我想得到这样的东西
This is a good doll www.google.com
我在 python 中尝试print re.sub(r'(http://|https://)',"",a))
了函数,但我只能从中删除http://
部分。关于如何在 python 2.7 中实现这一点的任何想法
>>> import re
>>> s = 'This is a good doll http://www.google.com/a/bs/jdd/etc/etc/a.py'
>>> re.sub(r'(?:https?://)([^/]+)(?:\S+)', r"\1", s)
'This is a good doll www.google.com'
如果你想使用正则表达式,那么你可以这样做:
>>> import re
>>> the_string = "This is a good doll http://www.google.com/a/bs/jdd/etc/etc/a.py"
>>> def replacement(match):
... return match.group(2)
...
>>> re.sub(r"(http://|https://)(.*?)/\S+", replacement, the_string)
'This is a good doll www.google.com'
>>> string = "This is a good doll http://www.google.com/a/bs/jdd/etc/etc/a.py"
>>> print string.replace('http://', '').split('/')[0]
This is a good doll www.google.com