Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 替换非数字字符_Python_Regex_String - Fatal编程技术网

Python 替换非数字字符

Python 替换非数字字符,python,regex,string,Python,Regex,String,我需要替换字符串中的非数字字符 例如,“8-4545-225-144”需要是“84545225144”;“$334fdf890==-”必须为“334890” 我如何才能做到这一点?使用正则表达式是可能的 ''.join(c for c in S if c.isdigit()) import re ... return re.sub(r'\D', '', theString) 尽管设置起来有点复杂,但根据我执行的计时测试,使用translate()string方法删除字符(如下所示)比使用

我需要替换字符串中的非数字字符

例如,“8-4545-225-144”需要是“84545225144”;“$334fdf890==-”必须为“334890”


我如何才能做到这一点?

使用正则表达式是可能的

''.join(c for c in S if c.isdigit())
import re

...

return re.sub(r'\D', '', theString)

尽管设置起来有点复杂,但根据我执行的计时测试,使用
translate()
string方法删除字符(如下所示)比使用
join()
re.sub()
快4-6倍——因此,如果是多次执行的操作,您可能需要考虑使用此选项。

nonnumerics = ''.join(c for c in ''.join(chr(i) for i in range(256)) if not c.isdigit())

astring = '123-$ab #6789'
print astring.translate(None, nonnumerics)
# 1236789

我更喜欢正则表达式,所以如果你喜欢,这里有一种方法

import re
myStr = '$334fdf890==-'
digts = re.sub('[^0-9]','',myStr) 

这将用“”替换所有非数字出现,即不使用任何内容。因此,digts变量应该是“334890”

让我们对
连接和
re
版本计时:

In [3]: import re

In [4]: def withRe(theString): return re.sub('\D', '', theString)
   ...:

In [5]:

In [6]: def withJoin(S): return ''.join(c for c in S if c.isdigit())
   ...:


In [11]: s = "8-4545-225-144"

In [12]: %timeit withJoin(s)
100000 loops, best of 3: 6.89 us per loop

In [13]: %timeit withRe(s)
100000 loops, best of 3: 4.77 us per loop
join
版本比
re
版本好得多,但不幸的是速度慢了50%。因此,如果性能是一个问题,可能需要牺牲优雅

编辑

In [16]: def withFilter(s): return filter(str.isdigit, s)
   ....:
In [19]: %timeit withFilter(s)
100000 loops, best of 3: 2.75 us per loop
看起来,
filter
是性能和可读性的赢家

filter(str.isdigit,s)
比这里列出的任何东西都更快、更清晰

如果s是unicode类型,它还将抛出TypeError。根据您需要的“数字”定义,这可能比可选的
过滤器(type(s).isdigit,s)
更有用,稍微慢一些,但对我来说比re和comprehension版本更快


Edit:尽管如果你是一个被Python3困住的可怜虫,你将需要使用
”.join(filter(str.isdigit,s))
,这会让你陷入相当糟糕的性能领域。这样的进步。

我讨厌当一个生成器表达式是正确的选项时,人们对列表的理解力会被猛击。先生,您赢得了一个互联网(和一张升级票)。+1代表非正则表达式解决方案。我讨厌人们在任何事情上都使用正则表达式,有些目标更适合非正则表达式solution@killown:我个人认为regex选项更具可读性。也许是因为我对正则表达式做了很多工作,但看看@KennyTM的代码,我马上就能看到它的作用;我花了一秒钟的时间才明白。str.translate方法比string.translate更可取。@Roger Pate,说得好,旧习惯很难改掉。。。我已经相应地更新了答案中的代码。感谢您指出这一点,并允许我改进示例代码。