Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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,我知道如何在两个标记之间捕获子字符串的单个实例: 我用以下字符串测试了该方法: text = 'blah.blah${capture1}.${capture2}' 我想得到这些标记“${”和“}”之间的所有子字符串,但它只得到第一个 >>text='blah.blah${capture1}.${capture2}' >>>found=re.search('\$\{(.+?)\}',text) >>>找到。组() (‘capture1’,) >>>len(找到了.groups()) 1. >>

我知道如何在两个标记之间捕获子字符串的单个实例:

我用以下字符串测试了该方法:

text = 'blah.blah${capture1}.${capture2}'
我想得到这些标记“${”和“}”之间的所有子字符串,但它只得到第一个

>>text='blah.blah${capture1}.${capture2}'
>>>found=re.search('\$\{(.+?)\}',text)
>>>找到。组()
(‘capture1’,)
>>>len(找到了.groups())
1.
>>> 

如何获取所有匹配项?

您需要一个正则表达式方法来查找字符串中的所有匹配项。您应该尝试
re.findall('\$\{(.+?)\}',text)
re.finditer('\$\{(.+?)\}',text)
。第一个将返回一个列表,第二个将返回一个iterable