我知道,*
是0以上,并且+
是1或更多,但如果我想要的东西,表示2个或多个(大于1)?
例如,我有
>>> y = 'U0_0, P33, AVG'
>>> re.findall(r'[a-zA-Z]+', y)
['U', 'P', 'AVG']
但是我只想获取包含2个或更多字母的字母。在此示例中,AVG
。
我该怎么做呢?
y = 'U0_0, P33, AVG'
print re.findall(r'[a-zA-Z]{2,}', y)
^^^
{m,n} Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 'a' characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match aaaab or a thousand 'a' characters followed by a b, but not aaab. The comma may not be omitted or the modifier would be confused with the previously described form.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句