。你能告诉我怎么做吗:
我有一根绳子:

str = "[someString xpos=1024.0 ypos=768.0 someotherString]"

我想从字符串中提取两个浮点数,这样就不能将它们用作函数的参数。
有什么简单的方法可以让我这样做吗?
在我看来是这样的:
*xpos=____.__*ypos=____.___*

哪里:
*: Any char

_: float number, maybe with variable lenght

(对不起,我的英语很差,有德语的正则表达式手册吗?)
当做

最佳答案

如果您的xposypos为负数,此选项将起作用:

x, y = map(float, re.findall(r'[+-]?[0-9.]+', str))

关于python - 使用正则表达式从字符串中提取浮点数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18177285/

10-12 21:22