我正在尝试使用正则表达式过滤字符串列表,就像这个答案中所示。然而代码给出了意外的结果:
In [123]: r = re.compile('[0-9]*')
In [124]: string_list = ['123', 'a', '467','a2_2','322','21']
In [125]: filter(r.match, string_list)
Out[125]: ['123', 'a', '467', 'a2_2', '322_2', '21']
我期望的输出结果是['123','467','21']
。
1e6
,会发生什么呢? :) - user1467267>>> '23e1'.isdigit()
是False
。 - Mike Vella"23".isdigit()
应该为false。我只期望匹配模式^[0-9]$
的字符串才为true。 - vlad_tepesch