How To Limit Regex's Findall() Method
Is there a regex equivalent of BeautifulSoup's limit=X argument for the findall method? I mean, how to find the first X words in question and then break the code execution? thank y
Solution 1:
Use re.finditer
and itertools.islice
:
from itertools import islice
import re
limit = 2
for x in islice(re.finditer(r'\d+', '1 2 33'), limit):
print(x.group())
As a function:
def findall_limiter(pattern, string, flags=0):
return islice(re.finditer(pattern, string, flags), limit)
eg.
for match in findall_limiter(r'\d+', '1 2 33', 2):
# do stuff
Solution 2:
You can use re.finditer
as it returns an iterator instead of generating all the values at once:
In [21]: strs="12345678"
In [22]: it=re.finditer("\d",strs)
In [23]: [next(it).group(0) for _ in xrange(4)] #returns only 4 mathces
Out[23]: ['1', '2', '3', '4']
Though this might raise StopIteration
error when the limit is greater than the number of matches. A simple workaround is to use exception handling or use itertools.isclice
:
In [26]: def limiter(strs,pattern,limit):
it=re.finditer(pattern,strs)
try:
for _ in xrange(limit):
yield next(it).group(0)
except StopIteration:
pass
....:
In [27]: list(limiter("12345","\d",3))
Out[27]: ['1', '2', '3']
In [28]: list(limiter("12345","\d",6))
Out[28]: ['1', '2', '3', '4', '5']
In [29]: list(limiter("12345","\d",10))
Out[29]: ['1', '2', '3', '4', '5']
help on re.finditer
:
In [24]: re.finditer?
Type: function
String Form:<function finditer at 0xb74114c4>
File: /usr/lib/python2.7/re.py
Definition: re.finditer(pattern, string, flags=0)
Docstring:
Return an iterator over all non-overlapping matches in the
string. For each match, the iterator returns a match object.
Empty matches are included in the result.
Post a Comment for "How To Limit Regex's Findall() Method"