(Python) 如何从字符串中提取数字(没有正则表达式)?

Bri*_*tPy 4 python string

我想提取字符串中包含的所有数字。我不能使用正则表达式,还有其他方法吗?

例子:

minput = "BLP45PP32AMPY"
Run Code Online (Sandbox Code Playgroud)

结果:

4532
Run Code Online (Sandbox Code Playgroud)

And*_*ely 6

您可以使用str.isnumeric

minput = "BLP45PP32AMPY"

number = int("".join(ch for ch in minput if ch.isnumeric()))
print(number)
Run Code Online (Sandbox Code Playgroud)

印刷:

4532
Run Code Online (Sandbox Code Playgroud)