我有以下组织模式语法:
** 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
如你所见,它与最后一行不符。你知道吗
我使用的是python2.7和findall。你知道吗
如果确定项目中不存在字符
*
,则可以使用:你可以通过
看看working on ideone.com。
代码的核心是(浓缩的)表达式:
在实际代码的
**
之后插入搜索字符串。请参见regex101.com上的Reading - Technology
演示。作为后续操作,您也可以只返回所选值,如下所示:
不确定整场比赛都需要正则表达式。我只需要使用正则表达式来匹配
**
行,然后返回行,直到看到下一行**
。你知道吗像这样的
相关问题 更多 >
编程相关推荐