Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用正则表达式中变量的正则表达式作为整数_Python_Regex - Fatal编程技术网

Python 如何使用正则表达式中变量的正则表达式作为整数

Python 如何使用正则表达式中变量的正则表达式作为整数,python,regex,Python,Regex,我正在使用一个xml解析器(来自lxml导入etree)来获取一个用java脚本编写的正则表达式。所以我把它们作为字符串来获取 例: 那么,我如何获得所有数字的匹配,比如714103145149120…。让我试着读懂你的想法-你有一个正则表达式模式作为字符串,你想用它在另一个字符串中查找匹配项?您可以直接使用您的模式: import re pattern = "7|14|103|14[598]|12[0-9]" # your fetched regex pattern target = "7

我正在使用一个xml解析器(
来自lxml导入etree
)来获取一个用java脚本编写的正则表达式。所以我把它们作为字符串来获取

例:


那么,我如何获得所有数字的匹配,比如
714103145149120

。让我试着读懂你的想法-你有一个正则表达式模式作为字符串,你想用它在另一个字符串中查找匹配项?您可以直接使用您的模式:

import re

pattern = "7|14|103|14[598]|12[0-9]"  # your fetched regex pattern
target = "7 14 103 145 149 120..."  # a text to match against

print(re.findall(pattern, target))  # or re.match() if you just need a match or more info
# ['7', '14', '103', '14', '14', '120']

然而,问题在于模式本身,因为它从左到右搜索,所以当它前面的模式中有
14
时,您将不会得到
14[598]
匹配-使该位冗余。再说一次,如果您从外部源获取regex模式,您在控制它的方式上不会获得太多信息。

在“如何获取匹配项”下,您是什么意思?匹配什么?有没有一种方法可以使用像数字103145这样的整数来匹配模式,而不是字符串“103”,“145”。当我尝试将它与整数匹配时,它要求的是字符串input@Shanmukh-您可以将整数转换为字符串并执行匹配。
import re

pattern = "7|14|103|14[598]|12[0-9]"  # your fetched regex pattern
target = "7 14 103 145 149 120..."  # a text to match against

print(re.findall(pattern, target))  # or re.match() if you just need a match or more info
# ['7', '14', '103', '14', '14', '120']