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 - Fatal编程技术网

在Python中消除字符串中的数字

在Python中消除字符串中的数字,python,regex,Python,Regex,我想在Python中消除字符串中的数字 str = "aaaa22222111111kkkkk" 我希望这是“aakkkk” 我使用re.sub替换,但它不起作用: str = "aaaa22222111111kkkkk" str = re.sub(r'^[0-9]+$',"",str) 也许,这会将只包含数字的字符串替换为“” 我该如何处理此问题?您的正则表达式错误: re.sub(r'[0-9]',"",str) 应: >>> str="aaaa22222111111

我想在Python中消除字符串中的数字

str = "aaaa22222111111kkkkk"
我希望这是
“aakkkk”

我使用
re.sub
替换,但它不起作用:

str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
也许,这会将只包含数字的字符串替换为

我该如何处理此问题?

您的正则表达式错误:

re.sub(r'[0-9]',"",str)
应:

>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

您不需要
^
$
。。。。或者
+
符号与IMO不完全相同,因为这是关于regex的。相关:这不是同一个问题。OP的疑虑也不同。被提名重开谢谢。我发现了什么问题。但这个问题似乎有争议。我不知道该怎么做,所以我就让它过去。str是一个不好的变量名