我需要替换https://www。、http://www.,http://和https://对以下输入使用Python3 regex;
输入:
https://www.example.com/
http://www.example.com/
https://example.com/
http://example.com/所需产出:
example.com既然我是Python的初学者,有人能帮我吗?
发布于 2018-10-31 16:12:02
非常感谢你的建议和帮助。在阅读regex教程之后,我成功地编写了下面的代码,这给了我预期的结果。
import re
domain = input("Enter the domain name : ")
stripped =
re.sub(r'https://www\.|http://www\.|https://www|http://|https://|www\.', "",
domain).strip('/')
print(stripped)发布于 2018-10-29 13:46:30
import re
string = "http://www.example.com"
pat = re.compile("https?://www.")
print(len(string))
val = pat.match(string)
if not val is None:
print(string[val.end():])
else:
print("no match")如果适用于you.if,您应该开始理解python中的正则表达式和最后一次匹配的end返回索引。
如果没有匹配,则val为None。
发布于 2018-10-29 14:40:46
>>> import re
>>> m = re.search('example.com', a)
>>> if m:
... print(m.group(0))其中,a是文本变量。
https://stackoverflow.com/questions/53045834
复制相似问题