gpt4 book ai didi

python - 问题 : throwing exception in re. 匹配函数

转载 作者:行者123 更新时间:2023-11-28 20:52:37 25 4
gpt4 key购买 nike

我有一个关于在 re.match 函数中抛出异常的简单问题

有没有办法“匹配”函数抛出的异常?也许一些旗帜。如果这是不可能的,也许 RE 库中有一些其他函数会在列表为空时抛出异常。

例子:

verify_link=re.match(".*"+str(country).lower()+"."+str(city).lower()+".*",link)

这个例子在某些情况下返回 None

我正在寻找一种会抛出 Exception 的方法,而不是 None我可以通过“if”语句来做到这一点,但也许还有其他方法

最佳答案

Python re 模块中没有这样的函数。查看doc :

exception re.error

Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.

关于python - 问题 : throwing exception in re. 匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6437422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com