首先我要告诉你我正在开发 Python 3.3。
我从 python 和正则表达式开始,我想知道如何从 2 个正则表达式之间的.txt文件中捕获文本块。
这是我的文件中的一个示例:
commit 1f0883381054b796b643dcff974435633eed8a79
this is
commit 1
bloc
commit 2f0883381054b796b643dcff974435633eed8a78
this is
commit 2
bloc
commit 3f0883381054b796b643dcff974435633eed8a77
this is
commit 3
bloc
commit 4f0883381054b796b643dcff974435633eed8a76
this is
commit
4
bloc
所以我想捕捉 2 个正则表达式之间的文本,以commit
AND 开头,后跟一个空格和 40 个字符(我想这是 : ^commit.{41}
)。NB 一条线刚开始,commit
后面没有任何东西不应该工作。当然,我必须能够获得最后一个提交块。它不会以 a 结尾,commit.{41}
而是以文件结尾。
一旦我完成了所有的块,我就必须能够处理它。这是 a 的git log -p
样子
commit 1f0883381054b796b643dcff974435633eed8a79
Merge: 4e1d5f7 8ffg9do
Author: name <email>
Date: date of commit
"comment
of the commit
on multilines"
diff --…
index …
--- …
+++ …
@@ …
-…
+…
…
diff --…
index …
--- …
+++ …
@@ …
commit 2f0883381054b796b643dcff974435633eed8a78
Author: name <email>
Date: date
例如,得到commitBlock[0]
. 那将是:
Merge: 4e1d5f7 8ffg9do
Author: name <email>
Date: date of commit
"comment
of the commit
on multilines"
diff --…
index …
--- …
+++ …
@@ …
-…
+…
…
diff --…
index …
--- …
+++ …
@@ …
提取Author:
线 (= Author: name <email>
)
提取注释commitBlock
:“多行提交的注释”
对于diffBlock
.
NB 和 一样commitBlock
,diffBlock
当有 another diff
or a commit.{41}
or a时应该停止end of file
我尝试了几件事:
这是我在意识到我需要获得 multines 块之前所拥有的。
source = open("file.txt","rt", encoding="ISO-8859-1")
for line in source:
commit = re.findall('^commit.{41}',line) #line starts with by "commit" and is followed by 41 characters
merge = re.findall('^Merge:.*',line)
author = re.findall('^Author:.*',line)
date = re.findall('^Date:.*',line)
signed = re.findall('Signed-off-by:.*', line)
for commitLine in commit:
print (commitLine)
#post into DB
for mergeLine in merge:
print (mergeLine)
#post into DB
.
.
.
或 re.findall('^commit.{41}(.*?)^commit.{41}| endofile ', source.read(), re.DOTALL|re.M)
我也尝试使用re.split()
. 它适用于commitBlock
!但是当我想拆分时我遇到了问题diffBlock
,commentBlock
因为我有时应该使用这commit
条线来停止阻塞。它不再出现,因为split
.
import os
import re
from pymongo import Connection
source = open("testSelection.txt","r", encoding="ISO-8859-1") #file that we want to analyse
sourceRead = open("testSelection.txt","r", encoding="ISO-8859-1").read() #writing source.read() bugs...
print(source.name)
commit = []
for line in source:
if line[:6]=="commit":
commitId = line[7:]
commit.append(line)
f=1
while f < len(commitBlock):
lineCommitBlock = re.split('\n', commitBlock[f])
diffBlock = re.split('\ndiff', commitBlock[f])
print("-----------------------------NEW COMMIT BLOCK-------------------------------")
print(commit[f-1])
i=0
while i < len(lineCommitBlock):
if "Merge:" in lineCommitBlock[i]:
print(lineCommitBlock[i])
elif "Author:" in lineCommitBlock[i]:
print(lineCommitBlock[i])
elif "Date:" in lineCommitBlock[i]:
print(lineCommitBlock[i])
elif "Signed-off-by:" in lineCommitBlock[i]:
print(lineCommitBlock[i])
elif "Tested-by:" in lineCommitBlock[i]:
print(lineCommitBlock[i])
elif "Reviewed-by:" in lineCommitBlock[i]:
print(lineCommitBlock[i])
i += 1
print("Before commentBlock <--------------------------------------------------------------------")
print("Adter commentBlock <--------------------------------------------------------------------")
j=1
while j < len(diffBlock):
print(diffBlock[j])
j += 1
f += 1
source.close()
(我同意,它看起来一团糟!)
知道如何解决我的问题吗?谢谢 !
编辑:
我几乎完成了我的工作,所以我想我找到使用正则表达式或其他东西而不是使用 GitPython(适用于 Python 2.,而不是 3....)的方法会更快。
有人可以帮我解决这个问题吗?
我所需要的只是在文件的开头Date:
和 adiff
或 acommit
或结尾的行之后捕获文本。
我真的卡在了这一点上......