我需要找到格式为“数字编号”的数字范围。数字应在0-3000范围内。所以我想出了这个正则表达式
match = re.search(r'^[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]-[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]',sentence)
当我运行程序时,我只想提取句子中的56-900,但程序提取了其他数字,如2016、CLP2012等。。我只想提取中间有“-”的数字。我的模式有什么问题。在
这段代码只提取一个真正的范围}
x-y
和{此示例的输出:
^{pr2}$使用python包regex_engine为数值范围生成正则表达式
您可以使用pip安装这个包
还可以为浮点和负范围生成正则表达式
^{pr2}$如果要匹配整数范围,则需要用r“\b”(字符串的开始/结束)来保护匹配项:
如果只想匹配0到3000之间的整数,则需要更精确的正则表达式,如下所示:
^{pr2}$相关问题 更多 >
编程相关推荐