【发布时间】:2013-07-03 14:10:28
【问题描述】:
我遇到了一些可能非常简单的问题,但我找不到解决方案。几天以来我一直在使用 Python,我需要使用正则表达式来获取文件的一部分。
我将git log -p 的结果放入一个文件中,现在我想提取一些信息。我唯一无法提取的是评论块。
此块位于:日期线与(差异线或列表末尾)之间。
...
Date: Wed Jul 3 22:32:36 2013 +0200
Here is the comment
of a commit
and I have to
extract it
diff --git a/dir.c b/dir.c
...
...
Date: Wed Jul 3 22:32:36 2013 +0200
Here is the comment
of a commit
and I have to
extract it
所以我尝试这样做:
commentBlock = re.compile("(?<=Date:.{32}\n\n).+(?=|\n\ndiff)", re.M|re.DOTALL)
findCommentBlock = re.findall(commentBlock,commitBlock[i]) # I've splited my git log everytime I find a "commit" line.
问题是:
- 日期线的长度可以改变。如果日期在 1 日到 9 日之间,则可以是
Date:.{32};如果日期是 2 个数字,则可以是Date:.{33}。 - 我不知道怎么说:“当一行以
diff开头或当它是列表(或文件)的末尾时,注释块停止”。
附:我正在使用 Python 3.x,我几乎完成了我的脚本,所以我真的不想使用像 GitPython 这样的特定工具(仅适用于 2.x)
【问题讨论】:
标签: python regex python-3.x