我有一个字符串:
link = "http://www.this_is_my_perfect_url.com/blah_blah/blah_blah?=trololo"
我有一个函数,返回域名从该网址或如果它没有找到,返回''
:
def get_domain(url):
domain_regex = re.compile("\:\/\/(.*?)\/|$")
return re.findall(domain_regex, str(url))[0].replace('www.', '')
get_domain(link)
返回结果:
this_is_my_perfect_url.com
|$
返回''
,如果正则表达式不匹配任何内容。
有没有办法在正则表达式中实现默认值Error
,这样我就不必在函数中做任何检查了?
如果link = "there_is_no_domain_in_here"
,则函数返回Error
,而不是''
。
3条答案
按热度按时间hmae6n7t1#
正如上面的注解中提到的,您不能在regex中设置任何内容来为您执行此操作,但是您可以检查
re.findall
在应用额外格式后返回的输出是否为空,如果为空,则意味着没有找到匹配,返回Error
输出将为
ma8fv8wu2#
我只想说一句--懒惰量词(
.*?
)和交替词(|$
)结合使用是非常低效的。此外,从
Python 3.8
开始,您可以使用walrus运算符,如下所示不,仅仅使用正则表达式,你不能从一个根本不存在的字符串中得到东西。
xpszyzbs3#
为什么不使用urlparse来获取域呢?