Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 从finditer获取范围和匹配_Python_Regex_Python 3.x - Fatal编程技术网

Python 从finditer获取范围和匹配

Python 从finditer获取范围和匹配,python,regex,python-3.x,Python,Regex,Python 3.x,我想获得span中的元组和macht的str,但是我不知道如何访问这个对象,文档中的方法不起作用 import re s = 'sd(asdf(xf))fg' if re.findall('\([A-z ]+\)', s): m = re.finditer('\([A-z ]+\)', s) m = list(m) print(m) 我得到这个输出: [<_sre.SRE_Match object; span=(7, 11), match='(xf)'>]

我想获得span中的元组和macht的str,但是我不知道如何访问这个对象,文档中的方法不起作用

import re

s = 'sd(asdf(xf))fg'

if re.findall('\([A-z ]+\)', s):

    m = re.finditer('\([A-z ]+\)', s)
    m = list(m)

print(m)
我得到这个输出:

[<_sre.SRE_Match object; span=(7, 11), match='(xf)'>]
[]
get(7,11)和“(xf)”需要什么过程或方法?

像这样尝试:

import re

s = 'sd(asdf(xf))fg'

for x in re.finditer('\([A-z ]+\)', s):
    print((x.start(), x.end()), x.group())

#output

(7, 11) (xf)

你救了我的命,谢谢,我很高兴能帮上忙!