How do you retain text offsets for every matching token in an evaluation log?

finditer yields match objects with both the matched text and a half-open character span.

Python example and interpretation

Extract two error codes from 错 E12 E7 and retain their positions. They are Python string indices, not UTF-8 byte offsets.

import re
[(m.group(),m.span()) for m in re.finditer(r'E[0-9]+','错 E12 E7')]

Result [('E12', (2, 5)), ('E7', (6, 8))]

中文

怎样从评测日志中提取每次命中的原文位置?

finditer 返回匹配对象的迭代器,可同时读取命中文本与左闭右开的字符区间。

Python 算例与适用条件

在“错 E12 E7”中提取两条错误码并记录起止位置。这些是 Python 字符串下标,不能直接当 UTF-8 字节偏移。

import re
[(m.group(),m.span()) for m in re.finditer(r'E[0-9]+','错 E12 E7')]

结果 [('E12', (2, 5)), ('E7', (6, 8))]

References / 参考来源

Keep reading / 继续阅读