import re
string = 'aaa(a)(123))((duoci))(234)ccc(567)zzzz'
p1 = re.compile(r'[(](.*?)[)]', re.S) # 最小匹配
print("."*20)
print(re.S)
print(p1)
print(type(p1))
print("."*20)
for i in re.finditer(p1, string):
print(i)
# print(type(i))
print(i.group())
print(i.groups())
print(i.span())
输出如下
....................
RegexFlag.DOTALL
re.compile('[(](.*?)[)]', re.DOTALL)
<class 're.Pattern'>
....................
<re.Match object; span=(3, 6), match='(a)'>
(a)
('a',)
(3, 6)
<re.Match object; span=(6, 11), match='(123)'>
(123)
('123',)
(6, 11)
<re.Match object; span=(12, 20), match='((duoci)'>
((duoci)
('(duoci',)
(12, 20)
<re.Match object; span=(21, 26), match='(234)'>
(234)
('234',)
(21, 26)
<re.Match object; span=(29, 34), match='(567)'>
(567)
('567',)
(29, 34)
Process finished with exit code 0