How do you audit how many spans a text-cleaning rule changed?
subn returns both the transformed text and the number of substitutions, allowing a cleaner to record its reach.
Python example and interpretation
Masking two run identifiers yields two substitutions. The count measures matched spans rather than the number of characters changed.
import re
re.subn(r'run-[0-9]+','run-ID','run-17 then run-23')Result ('run-ID then run-ID', 2)
中文
怎样核对文本清洗规则实际替换了几处?
subn 同时返回替换后的字符串与执行替换的次数,便于把规则的作用范围纳入日志。
Python 算例与适用条件
把两个 run 编号统一写为 run-ID,计数应为 2。替换次数按匹配片段计算,和改动的字符数不同。
import re
re.subn(r'run-[0-9]+','run-ID','run-17 then run-23')结果 ('run-ID then run-ID', 2)