regex 正则表达式缺少匹配项

ax6ht2ek  于 2023-05-08  发布在  其他
关注(0)|答案(2)|浏览(124)

我有下面的代码来指定我的Python正则表达式

import re                            # Import the regular expressions package
import matplotlib.pyplot as plt      # Import the matplotlib package
  
# Read the log file
with open('myfile.log', 'rt',encoding="latin-1") as file:
    log_text = file.read()

# Define regular expressions for pattern matching
pattern = r'Function eval (\d+) at point \d+ has f = ([-\d.]+) at x = \[ ([-\d.]+) ([-\d.]+) ([-\d.]+)\]'

# Extract data using regular expressions
matches = re.findall(pattern, log_text)

# Extract eval_numbers and f_values
eval_numbers = [int(match[0]) for match in matches]
f_values = [-float(match[1]) for match in matches]

我的日志文件如下所示

Function eval 1 at point 1 has f = -0.258940971315798 at x = [ 3.  14.4 -6.5]
Initialising (coordinate directions)
Function eval 2 at point 2 has f = -0.2259040076585 at x = [ 3.5 14.4 -6.5]
Function eval 3 at point 3 has f = -0.258912721121113 at x = [ 3.  15.9 -6.5]
Function eval 4 at point 4 has f = -0.259008279995406 at x = [ 3.  14.4 -5.8]
Function eval 5 at point 5 has f = -0.305466875532906 at x = [ 2.5 14.4 -6.5]
Function eval 6 at point 6 has f = -0.258922045649132 at x = [ 3.  12.9 -6.5]
Function eval 7 at point 7 has f = -0.258959151282265 at x = [ 3.  14.4 -7.2]
Beginning main loop
Function eval 8 at point 8 has f = -0.34817783367277 at x = [ 2.00000006 14.39986872 -6.49967721]
Function eval 9 at point 9 has f = -0.562108249792704 at x = [ 5.72367526e-08  1.43999223e+01 -6.49980500e+00]
New rho = 0.01 after 9 function evaluations
Function eval 10 at point 10 has f = -0.325941420427775 at x = [ 0.07158739 14.41254181 -6.5305591 ]
Function eval 11 at point 11 has f = -0.562106040838141 at x = [ 0.         14.35760572 -6.39667873]
Function eval 12 at point 12 has f = -0.308741672869589 at x = [ 6.97916734e-03  1.45423240e+01 -6.48009766e+00]
Function eval 13 at point 13 has f = -0.562119294825946 at x = [ 0.         14.25262353 -6.51302969]
Function eval 14 at point 14 has f = -0.322997122706926 at x = [ 0.04000959 14.17489323 -6.53416455]
Function eval 15 at point 15 has f = -0.562174526503614 at x = [ 0.         14.34595835 -6.56782799]
Function eval 16 at point 16 has f = -0.562187783721323 at x = [ 0.         14.32118157 -6.49878953]
Function eval 17 at point 17 has f = -0.562178945506711 at x = [ 0.         14.20668383 -6.45356794]
New rho = 0.001 after 17 function evaluations
Function eval 18 at point 18 has f = -0.562150223228322 at x = [ 0.         14.3725033  -6.52431188]
Function eval 19 at point 19 has f = -0.30923561054078 at x = [ 8.00416661e-03  1.43249728e+01 -6.49891060e+00]
Function eval 20 at point 20 has f = -0.562145804676754 at x = [ 0.         14.33349049 -6.48225911]
Function eval 21 at point 21 has f = -0.564667972452139 at x = [ 3.44362129e-03  1.43268836e+01 -6.49446789e+00]
Function eval 22 at point 22 has f = -0.564476463041547 at x = [ 3.17296828e-03  1.43150903e+01 -6.49015891e+00]
New rho = 0.0001 after 22 function evaluations
Function eval 23 at point 23 has f = -0.563516144031397 at x = [ 1.78656684e-03  1.43313574e+01 -6.49288379e+00]
Function eval 24 at point 24 has f = -0.564680143588853 at x = [ 3.47138233e-03  1.43297133e+01 -6.49561561e+00]
Function eval 25 at point 25 has f = -0.564590127687801 at x = [ 3.30886293e-03  1.43308568e+01 -6.49396456e+00]
Function eval 26 at point 26 has f = -0.564689063616869 at x = [ 3.51710529e-03  1.43310012e+01 -6.49596869e+00]
Function eval 27 at point 27 has f = -0.564386329819354 at x = [ 3.06544700e-03  1.43309346e+01 -6.49626736e+00]
Function eval 28 at point 28 has f = -0.564987691201283 at x = [ 3.97493644e-03  1.43315974e+01 -6.49592662e+00]
Function eval 29 at point 29 has f = -0.565043509875675 at x = [ 4.07612511e-03  1.43313188e+01 -6.49659970e+00]
Function eval 30 at point 30 has f = -0.565023413882358 at x = [ 4.02815339e-03  1.43322001e+01 -6.49716216e+00]
New rho = 1e-05 after 30 function evaluations

我想在每个函数求值时提取f值,但我似乎只得到随机返回的事件,如4,8,15等。
对于如何更好地定义正则表达式以捕获每个函数的求值,有什么想法吗?
我试过强制编码和文件处理为文本或二进制,但它没有区别。
日志文件由PyBobyQa优化日志输出创建。

5jdjgkvh

5jdjgkvh1#

在方括号内的输入中有多个连续的空格,但是regexp只匹配一个空格。您可能需要将某些空格替换为+*。此外,您应该添加+e作为数字中的有效字符。

pattern = r'Function eval (\d+) at point \d+ has f = ([-\d.]+) at x = \[ *([-+eE\d.]+) +([-+eE\d.]+) +([-+eE\d.]+) *\]'

一般来说,您可能希望使空白匹配更加宽松,以避免将来发生解析错误:

pattern = r'Function\s+eval\s+(\d+)\s+at\s+point\s+\d+\s+has\s+f\s*=\s*([-\d.]+)\s+at\s+x\s*=\s*\[\s*([-+eE\d.]+)\s+([-+eE\d.]+)\s+([-+eE\d.]+)\s*\]'
bqjvbblv

bqjvbblv2#

您的模式中与x匹配的部分有一些问题。首先,你只允许值之间有一个空格,而你的一些行有两个或更多。其次,不允许在值中使用指数(如eval 9)。最后(在eval 10)在结束]之前有一个空格。将正则表达式的这一部分更改为this将解决这个问题:

x = \[ ([-+\d.e]+) +([-+\d.e]+) +([-+\d.e]+) *\]

pattern = r'Function eval (\d+) at point \d+ has f = ([-\d.]+) at x = \[ ([-+\d.e]+) +([-+\d.e]+) +([-+\d.e]+) *\]'

对于文本的前10个评估,re.findall现在将返回

[
 ('1', '-0.258940971315798', '3.', '14.4', '-6.5'),
 ('2', '-0.2259040076585', '3.5', '14.4', '-6.5'),
 ('3', '-0.258912721121113', '3.', '15.9', '-6.5'),
 ('4', '-0.259008279995406', '3.', '14.4', '-5.8'),
 ('5', '-0.305466875532906', '2.5', '14.4', '-6.5'),
 ('6', '-0.258922045649132', '3.', '12.9', '-6.5'),
 ('7', '-0.258959151282265', '3.', '14.4', '-7.2'),
 ('8', '-0.34817783367277', '2.00000006', '14.39986872', '-6.49967721'),
 ('9', '-0.562108249792704', '5.72367526e-08', '1.43999223e+01', '-6.49980500e+00'),
 ('10', '-0.325941420427775', '0.07158739', '14.41254181', '-6.5305591')
]

相关问题