假设我有一个网址=“https://google.com/?q=cats”,那我该怎么搜索“://”和“?”并用空格替换它们?
import re
url="https://google.com/?q=cats"
re.search("\w+",url) # what should I include in this pattern to detect (:// and ?)
这应该排除你:
re.sub('://|\?', ' ', url)
#https google.com/ q=cats
>>> re.sub(r'(://|\?)', ' ', url)
'https google.com/ q=cats'