[英]How to define several float groups in a regular expression?
I would like to match a string such as : 我想匹配一个字符串,例如:
k-point 1 : 0.00000000 0.00000000 0.00000000 weight = 0.01562500
I wrote that regex : 我写了正则表达式:
kpt_patt = re.compile("^\s*k-point\s+(\d+) :\s+" +
"([+-]?\d+\.\d+)\s+" +
"([+-]?\d+\.\d+)\s+" +
"([+-]?\d+\.\d+)\s+" +
"weight = (\d+\.\d+)")
line = " k-point 1 : 0.00000000 0.00000000 0.00000000 weight = 0.01562500"
m = kpt_patt.match(line)
print(m.groups())
In [1]: m.groups()
Out[1]: ('1', '0.00000000', '0.00000000', '0.00000000', '0.01562500')
It works but is there a better way to said that I want this group ([+-]?\\d+\\.\\d+)\\s+
several times ? 它有效,但是有更好的方法来表示我要这个组
([+-]?\\d+\\.\\d+)\\s+
几次吗? Or a better way to write the whole regex of course. 或者是编写整个正则表达式的更好方法。
Use string multiplication. 使用字符串乘法。 I've added the
r
prefix to your regex strings. 我已经在您的正则表达式字符串中添加了
r
前缀。
float_rx = r"([+-]?\d+\.\d+)\s+"
kpt_patt = re.compile(r"^\s*k-point\s+(\d+) :\s+" +
float_rx * 3 +
r"weight = (\d+\.\d+)")
Also, your regex will not match numbers like .123
or 1
. 另外,您的正则表达式将不匹配
.123
或1
类的数字。 You may or may not care about this. 您可能会或可能不会在乎。 This is what I typically use to match a float:
r"(\\d+(?:\\.\\d*)?|\\.\\d+)"
. 这通常是我用来匹配浮点数的方法:
r"(\\d+(?:\\.\\d*)?|\\.\\d+)"
。 It will match numbers of these forms: 1
, 1.
, 1.1
, and .1
, without matching .
它将匹配的这些形式的数字:
1
, 1.
, 1.1
和.1
,而无需匹配.
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.