【发布时间】:2015-12-18 10:52:33
【问题描述】:
我有一个像这样的 todo.txt 列表,用换行符分隔:
(D) 2015-02-18 XDA Ultimate guide to +Tasker @Phone @Computer
2015-02-18 Redesign the business card for +RepairWork @Computer
(A) 2015-02-17 +Study how to +Ask questions @Computer @Phone
(B) 2015-03-25 Update +LaundryTimer W/ new popup design +Tasker
我有正则表达式来捕获 +Projects 和 @Contexts:
## Projects
project_matches = re.findall('[+]\D\w+',todo_list)
print list(set(project_matches))
## Contexts
context_matches = re.findall('[@][A-Z]\w+',todo_list)
print list(set(context_matches))
但我也想通过 +Project 或 @Context 快速有效地捕获每个任务和组。
例如,这是所需的输出:
Phone:
(A) 2015-02-17 +Study how to +Ask questions @Computer @Phone
(D) 2015-02-18 XDA Ultimate guide to +Tasker @Phone @Computer
Computer:
(D) 2015-02-18 XDA Ultimate guide to +Tasker @Phone @Computer
2015-02-18 Redesign the business card for +RepairWork @Computer
Tasker:
(D) 2015-02-18 XDA Ultimate guide to +Tasker @Phone @Computer
(B) 2015-03-25 Update +LaundryTimer W/ new popup design +Tasker
等等……
我也有 Regex 在找到项目或上下文时捕获任务,但我不知道它是否有帮助:(.*)(?=[+]\D\w+)(.*)
【问题讨论】: