I need limit re.findall to find first 3 matches and then stop.
for example
text = 'some1 text2 bla3 regex4 python5'
re.findall(r'\d',text)
then I get:
['1', '2', '3', '4', '5']
and I want:
['1', '2', '3']
I need limit re.findall to find first 3 matches and then stop.
for example
text = 'some1 text2 bla3 regex4 python5'
re.findall(r'\d',text)
then I get:
['1', '2', '3', '4', '5']
and I want:
['1', '2', '3']
To find N matches and stop, you could use re.finditer and itertools.islice:
>>> import itertools as IT
>>> [item.group() for item in IT.islice(re.finditer(r'\d', text), 3)]
['1', '2', '3']