我需要匹配字符串一樣:'2017-08-09,08:59:20.445 INFO {peers_peak_parameters_grid} [eval_peers_peak] Evaluating batch 0 out of 2158',
蟒蛇重新匹配字符串整數
我已經嘗試了不同的正則表達式,如:comp = re.compile("Evaluating batch ^[-+]?[0-9]+$ out of ^[-+]?[0-9]+$")
,這是一個例子用法:
def get_batch_process_time(log):
loglines = log.splitlines()
comp = re.compile("Evaluating batch ^[-+]?[0-9]+$ out of ^[-+]?[0-9]+$")
times = []
matches = []
for i, line in enumerate(loglines):
if comp.search(line):
time = string2datetime(line.split(' ')[0])
times.append(time)
matches.append(line)
return np.array(times), matches
不幸沒有一條線似乎符合給定的模式。我假設我使用了錯誤的正則表達式。
什麼是正確的正則表達式? 我正在使用re
嗎? (我應該使用匹配而不是搜索嗎?)
你的字符串沒有'out'。 ['評估批處理[ - +]?[0-9] +出[ - +]?[0-9] +'](https://regex101.com/r/B3hqgw/1)似乎工作,如果它確實。 –
抱歉在這個問題上有錯誤。我已編輯示例字符串 – Donbeo
使用此模式:'評估批[0-9] {4}中的[0-9] {4}' – hadi