我试图在一个字符串中得到一个参考号,在大多数情况下,它前面有“Ref”或类似的东西。在
例如:
Explorer II Ref.16570 Box
问题是有很多不同的变体1,因为这是用户生成的内容。我如何用python检索前面的数字,例如Ref.
?在
数字/字符串并不总是相同的模式,例如数字。它们可能与字符、圆点和斜杠混合在一起,但对于人眼来说,几乎每一行都有这样一个数字。在
例如:
Ref.16570
Ref. 16570
Referenz 216570
Referenz 01 733 7653 4159-07 4 26
331.12.42.51.01.002
166.0173
AB012012/BB01
Ref. 167.021
PAM00292
14000M
L3.642.4.56.6
161.559.50
801
666
753
116400GV
Ref.: 231.10.39.21.03.002
3233
Ref: 233.32.41.21.01.002
T081.420.97.057.01
16750
... almost each line in the example provided contains a certain ID
少量的假阳性不会是问题。在
不完全确定是否需要
match
或extract
,但Ref\.?([ \d.]+)
将提取Ref
之后的任何数字(不区分大小写),即:^{pr2}$
Regex Demo
Python Demo
正则表达式解释
尝试以下代码。它收集
Ref
之后的所有数据,直到一个预定义的塞子。使用句号是因为问题没有明确定义什么数据是引用(not always the same pattern
,might be mixed with
,for a human eye there is almost always
)。我想需要额外的匹配处理来更准确地提取实际引用。在输出从以下行开始:
^{pr2}$这应该能做到:
更多信息:
相关问题 更多 >
编程相关推荐