得到物品,我有以下org-模式语法:Python的正则表达式 - 从org-模式文件
** Hardware [0/1]
- [ ] adapt a programmable motor to a tripod to be used for panning
** Reading - Technology [1/6]
- [X] Introduction to Networking - Charles Severance
- [ ] A Tour of C++ - Bjarne Stroustrup
- [ ] C++ How to Program - Paul Deitel
- [X] Computer Systems - Randal Bryant
- [ ] The C programming language - Brian Kernighan
- [ ] Beginning Linux Programming -Matthew and Stones
** Reading - Health [3/4]
- [ ] Patrick McKeown - The Oxygen Advantage
- [X] Total Knee Health - Martin Koban
- [X] Supple Leopard - Kelly Starrett
- [X] Convict Conditioning 1 and 2
,我想提取的物品,如:
getitems "Hardware"
我应该得到:
- [ ] adapt a programmable motor to a tripod to be used for panning
如果我要 “读 - 健康”,我应该得到:
- [ ] Patrick McKeown - The Oxygen Advantage
- [X] Total Knee Health - Martin Koban
- [X] Supple Leopard - Kelly Starrett
- [X] Convict Conditioning 1 and 2
我现在用的是以下模式:
pattern = re.compile("\*\* "+ head + " (.+?)\*?$", re.DOTALL)
询问时输出 “读 - 技术” 是:
- [X] Introduction to Networking - Charles Severance
- [ ] A Tour of C++ - Bjarne Stroustrup
- [ ] C++ How to Program - Paul Deitel
- [X] Computer Systems - Randal Bryant
- [ ] The C programming language - Brian Kernighan
- [ ] Beginning Linux Programming -Matthew and Stones
** Reading - Health [3/4]
- [ ] Patrick McKeown - The Oxygen Advantage
- [X] Total Knee Health - Martin Koban
- [X] Supple Leopard - Kelly Starrett
- [X] Convict Conditioning 1 and 2
我也试过:
pattern = re.compile("\*\* "+ head + " (.+?)[\*|\z]", re.DOTALL)
这除了最后一个之外,最后一个工作正常。
输出要求时,“读 - 健康”:
- [ ] Patrick McKeown - The Oxygen Advantage
- [X] Total Knee Health - Martin Koban
- [X] Supple Leopard - Kelly Starrett
正如你所看到的,它不会在最后一行匹配。
我使用python 2.7,并findall。
'\ * \ *阅读 - 健康(*?)(?:\ * \ *。 | $)' – JazZ