我是真正的 python 初学者,但我听说它在字符串操作方面应该很容易。你能告诉我怎么做吗:
我有一个字符串:
str = "[someString xpos=1024.0 ypos=768.0 someotherString]"
我想从该字符串中提取两个 float ,这样我就不能将它们用作函数的参数。
有没有简单的方法可以做到这一点? 在我看来,它看起来像这样:
*xpos=____.__*ypos=____.___*
哪里:
*: Any char
_: float number, maybe with variable lenght
(对不起,我的英文很烂,有正则表达式的德文手册吗?)
问候
最佳答案
如果您的 xpos
或 ypos
为负数,这将起作用:
x, y = map(float, re.findall(r'[+-]?[0-9.]+', str))
关于python - 使用正则表达式从字符串中提取 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177285/