如何使用正则表达式删除字符串数字

How to remove string numbers with regex

我有这个字符串,我想删除具有两位小数和三位小数的数字,但是,对于包含两位小数的数字,我不想删除中的前两个数字字符串,这是我的代码。

import re
string = "air max 12 x clot infantil 16 26 67 80 272 117 160"
regex = re.sub(r"\d{3}", "", string)           
print(regex)

注意,我可以消除小数点后 3 位的数字,但不能消除小数点后两位的数字。即使我的代码是这样的:

import re
string = "air max 12 x clot infantil 16 26 67 80 272 117 160"
regex = re.sub(r"\d{2,3}", "", string)           
print(regex)

这行得通,问题是它会删除前两个有两位小数的数字,我想要的输出是:

import re
string = "air max 12 x clot infantil 16 26 67 80 272 117 160"
regex = re.sub(r"\d{2,3}", "", string)
//something here
print(regex)
Expected output
air max 12 x clot infantil

如何使用正则表达式执行此操作?

您可以使用以下正则表达式:'[a-zA-Z].*[a-zA-Z]',它将匹配从字母开始到字母结束的任何内容。

>>> re.findall('[a-zA-Z].*[a-zA-Z]', string)
['air max 12 x clot infantil']