0

假设我有一个字符串 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 中实现这一点的任何想法

4

3 回答 3

4
>>> 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'
于 2012-12-06T06:44:19.880 回答
2

如果你想使用正则表达式,那么你可以这样做:

>>> 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'
于 2012-12-06T06:46:16.440 回答
0
>>> 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
于 2012-12-06T06:38:50.727 回答