本文介绍了如何限制正则表达式的 findall() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于 findall
方法,是否有与 BeautifulSoup 的 limit=X
参数等效的正则表达式?我的意思是,如何找到有问题的前 X 个单词然后中断代码执行?谢谢
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 you
推荐答案
您可以使用 re.finditer
因为它返回一个迭代器而不是一次生成所有值:
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']
虽然这可能会在限制大于匹配数时引发 StopIteration
错误.一个简单的解决方法是使用异常处理或使用 itertools.isclice
:
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']
关于 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.
这篇关于如何限制正则表达式的 findall() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!