I don't understand why this regex only returns the last match:
import re
text = """
Chicken chicken chicken chicken chicken chicken.
#=================
# @title Chicken
# @author Me
#=================
Chicken chicken chicken.
"""
rx = r"#=+\n(?:#\s*@(\w+)\s+(.*)\n)+#=+"
for match in re.finditer( rx, text ):
print match.groups()
# Output:
# ('author', 'Me')
I would expect this regex to return [ ('title', 'Chicken'), ('author', 'Me') ]
, but it seems to only return the last match. This does not change if I set the flag re.M
(multiline), and the flag re.DOTALL
is not what I intend here.
For clarity, you can visualise the regex here, it seems to be what I intended, namely:
- From the first comment line
#===...
- Find and capture the next lines with the format
# @(word) (anything)