python检查字符串包含有效的正则表达式python

2024-03-29 13:04:28 发布

您现在位置:Python中文网/ 问答频道 /正文

为了检查字符串是否是有效的正则表达式python。在

示例:

asdasd="([0-9]{1})";APRI="([0-9]{9})";ADD="([0-9]{1})"

如何检查字符串是否包含有效的正则表达式?在

谢谢


Tags: 字符串add示例asdasdapri
3条回答

EAFP

编译一下看看。在

re.compile(asdasd)

在在线regex测试程序中尝试您的regex代码

https://regex101.com/

你的第一个例子: https://regex101.com/r/gN1tG8/1

应用EAFP approach编译表达式并处理错误。在

例如,不平衡括号:

>>> import re
>>> re.compile(r"(test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/.virtualenvs/so/lib/python2.7/re.py", line 194, in compile
    return _compile(pattern, flags)
  File "/Users/user/.virtualenvs/so/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

相关问题 更多 >