python regex re.compile and re.search


import re
real_comp = re.compile(r'[0-9]*')
real_comp.search('+123i').group()
Out[7]: '' 

我期望结果为" 123",但它返回空白。怎么了?

您需要另一个量词,即 +

import re
real_comp = re.compile(r'([0-9]+)')
print(real_comp.search('+123i').group())

产生

123

否则,Regex引擎在最初消耗的字符之前就报告了一场比赛([0-9]*始终是正确的)。

最新更新