gpt4 book ai didi

python - 用于查找方括号或圆括号内的 float 的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:51 25 4
gpt4 key购买 nike

我想在 python 中使用正则表达式找到以下模式[3.000, 3.000](1.07,24.96)

我需要找到方括号和圆括号以及 1 到 3 位 float

regex="^[(\[]/\d+\.\d+/,/\d+\.\d+/[)\]]$"

输出为空,未找到匹配项。

[(\[]- square/round braces
d+\.\d+/- decimal number
a comma and another decimal number
[)\]]- square/round closing

最佳答案

import re
print(re.match(r"^[([]\d+\.\d+,\s*\d+\.\d+[)\]]$", '(1.07,24.96)'))
  1. 对正则表达式使用原始字符串 (r"..."),这样斜杠和转义更有可能是正确的。
  2. / 不是一个具有任何特殊含义的字符,我不知道你为什么把它放在那里,但把它们全部去掉。
  3. \s* 的逗号后面允许有可选空格。

关于python - 用于查找方括号或圆括号内的 float 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54474434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com