python - python re 找不到这个分组名称
问题描述
我尝试就论文参考的格式提供建议。例如,对于学术论文,格式为:
author. dissertation name[D]. place where store it: organization who hold the copy, year in which the dissertation published.
显然,除了年份之外,每个项目中都可能有一些标点符号。例如
Smith. The paper name. The subtitle of paper[D]. United States: MIT, 2011
经常,place where store it
并且year
被错过,例如
Smith. The paper name. The subtitle of paper[D]. US, 2011
Smith. The paper name. The subtitle of paper[D]. US: MIT
我想这样编程:
import re
reObj = re.compile(
r'.*\[D\]\. \s* ((?P<PLACE>[^:]*):){0,1} \s* (?P<HOLDER>[^:]*) (?P<YEAR>,\s*(1|2)\d{3}){0,1}',
re.VERBOSE
)
txt = '''Smith. The paper name. The subtitle of paper[D]. US: MIT, 2011
Smith. The paper name. The subtitle of paper[D]. US, 2011
Smith. The paper name. The subtitle of paper[D]. US: MIT'''.split('\n')
for i in txt:
if reObj.search(i):
if reObj.search(i).group('PLACE')==None:
print('missing place')
if reObj.search(i).group('YEAR')==None:
print('missing year')
else:
print('bad formation')
但我发现在 txt 中没有为 i 获得 YEAR: print(i) print(reObj.search(i).group('HOLDER'))
输出
Smith. The paper name. The subtitle of paper[D]. US: MIT, 2011
MIT, 2011
Smith. The paper name. The subtitle of paper[D]. US, 2011
US, 2011
Smith. The paper name. The subtitle of paper[D]. US: MIT
MIT
for i in txt:
print(i)
print(reObj.search(i).group('YEAR'))
输出
Smith. The paper name. The subtitle of paper[D]. US: MIT, 2011
None
Smith. The paper name. The subtitle of paper[D]. US, 2011
None
Smith. The paper name. The subtitle of paper[D]. US: MIT
None
那么,为什么我的命名组失败以及如何修复它?谢谢
解决方案
在我看来你可以使用
reObj = re.compile("""
\[D\]\. \s* # [D]. and 0+ whitespaces
(?: # An optional alternation group
(?P<PLACE>[^,:]*) # Group "PLACE": 0+ chars other than , and :
(?: # An optional sequence of
: \s* (?P<HOLDER>[^,:]*) # :, 0+ whitespaces, Group "HOLDER" (0+ non-colons and non-commas)
)?
(?: # An optional sequence of
,\s* (?P<YEAR>[12]\d{3}) # , + 0+ whitespaces, Group "YEAR" (1 or 2 and then three digits
)?
)?
$ # end of string
""", flags=re.X)
import re
reObj = re.compile(
r"\[D\]\.\s*(?:(?P<PLACE>[^,:]*)(?::\s*(?P<HOLDER>[^,:]*))?(?:,\s*(?P<YEAR>[12]\d{3}))?)?$",
re.VERBOSE
)
txt = '''Smith. The paper name. The subtitle of paper[D]. US: MIT, 2011
Smith. The paper name. The subtitle of paper[D]. US, 2011
Smith. The paper name. The subtitle of paper[D]. US: MIT'''.split('\n')
for i in txt:
print('------------------------\nTESTING {}'.format(i))
m = reObj.search(i)
if m:
if not m.group('PLACE'):
print('missing place')
else:
print(m.group('PLACE'))
if not m.group('YEAR'):
print('missing year')
else:
print(m.group('YEAR'))
输出:
------------------------
TESTING Smith. The paper name. The subtitle of paper[D]. US: MIT, 2011
US
2011
------------------------
TESTING Smith. The paper name. The subtitle of paper[D]. US, 2011
US
2011
------------------------
TESTING Smith. The paper name. The subtitle of paper[D]. US: MIT
US
missing year
推荐阅读
- javascript - 捕获选项与 useCapture
- unity3d - Unity - 屏幕覆盖画布通过墙壁可见
- alexa - Alexa Reminderd API 未经授权
- python - 使用 selenium python(重新发布)在 Web 应用程序上执行单击
- lambda - 使用 JAVA 8 遍历附加到 ArrayList 的 MAP 条目元素
- swagger - Swagger 无法向 Azure AD B2C 进行身份验证
- react-native - 动态 textInput 重新渲染整个组件
- vue.js - Vue:如何动画列表重新排序?
- 3d - 如何分离 3D 计算机视觉和 3D 几何?他们每个人的子类别是什么
- merge - Ansible 按前缀合并列表和字典变量