【发布时间】:2019-05-06 19:48:43
【问题描述】:
我有一个如下所示的字符串
line="record of Students Name Codes: AC1.123 XYZ12.67 the student is math major first hisory: XY12.34 good performer second history M12.78 N23.76 faculty Miss Cooper"
我想从该行中提取一些代码。我正在使用下面的程序。我想忽略历史部分中的代码。
我可以知道如何忽略其中有历史记录的部分中的代码
import re
regular_expression = re.compile(r'\b[A-Z]+\d{1,2}\.*\d{1,2}\w{0,2}\b', re.I)
matches = regular_expression.findall(line)
for match in matches:
print (match)
预期输出
AC1.123
XYZ12.67
当前输出:
AC1.123
XYZ12.67
XY12.34
M12.78
N23.76
【问题讨论】:
-
这里如何定义块?为什么不先分块呢?
-
是的,我在想如何在这里定义块?
标签: regex python-3.x string