多行检查的正则表达式
我正在尝试使用正则表达式(import re
)从日志文件中提取我想要的信息。
更新:我添加了 C:\WINDOWS\security
文件夹的权限,这导致所有示例代码都无法运行。
假设日志的格式是:
C:\:
BUILTIN\Administrators Allowed: Full Control
NT AUTHORITY\SYSTEM Allowed: Full Control
BUILTIN\Users Allowed: Read & Execute
BUILTIN\Users Allowed: Special Permissions:
Create Folders
BUILTIN\Users Allowed: Special Permissions:
Create Files
\Everyone Allowed: Read & Execute
(No auditing)
C:\WINDOWS\system32:
BUILTIN\Users Allowed: Read & Execute
BUILTIN\Power Users Allowed: Modify
BUILTIN\Power Users Allowed: Special Permissions:
Delete
BUILTIN\Administrators Allowed: Full Control
NT AUTHORITY\SYSTEM Allowed: Full Control
(No auditing)
C:\WINDOWS\system32\config:
BUILTIN\Users Allowed: Read & Execute
BUILTIN\Power Users Allowed: Read & Execute
BUILTIN\Administrators Allowed: Full Control
NT AUTHORITY\SYSTEM Allowed: Full Control
(No auditing)
C:\WINDOWS\security:
BUILTIN\Users Allowed: Special Permissions:
Traverse Folder
Read Attributes
Read Permissions
BUILTIN\Power Users Allowed: Special Permissions:
Traverse Folder
Read Attributes
Read Permissions
BUILTIN\Administrators Allowed: Full Control
NT AUTHORITY\SYSTEM Allowed: Full Control
(No auditing)
这个格式在其他几个目录中也会重复。那么我该如何把它们分成 段落
,然后检查包含 Special Permissions:
的行呢?
像这样:
- 把整个字符串分成几个部分,比如
C:\
和C:\WINDOWS\system32
。 - 在每一行中查找包含 'Special Permissions:' 的内容。
- 显示整行内容,例如:
C:\:
BUILTIN\Users 允许: 特殊权限: \n\
创建文件夹\n\
BUILTIN\Users 允许: 特殊权限: \n\
创建文件\n\
- 对下一个 '段落' 重复以上步骤。
我在想:
- 在整个文本文件中搜索
r"(\w+:\\)(\w+\\?)*:"
- 这样可以返回路径。 - 使用字符串函数或正则表达式获取其余的输出。
- 去掉除了包含
Special Permissions
的行以外的所有其他行。 - 显示结果,然后重复第一步。
但我觉得这样效率不高。
有没有人能指导我一下?谢谢。
示例输出:
C:\:
BUILTIN\Users Allowed: Special Permissions:
Create Folders
BUILTIN\Users Allowed: Special Permissions:
Create Files
C:\WINDOWS\system32:
BUILTIN\Power Users Allowed: Special Permissions:
Delete
C:\WINDOWS\security:
BUILTIN\Users Allowed: Special Permissions:
Traverse Folder
Read Attributes
Read Permissions
BUILTIN\Power Users Allowed: Special Permissions:
Traverse Folder
Read Attributes
Read Permissions
C:\WINDOWS\system32\config
没有显示,因为这些行中没有特殊权限。
我使用的模板:
import re
text = ""
def main():
f = open('DirectoryPermissions.xls', 'r')
global text
for line in f:
text = text + line
f.close
print text
def regex():
global text
<insert code here>
if __name__ == '__main__':
main()
regex()
5 个回答
1
这段话的意思是,跟milkypostman的解决方案类似,但它的输出格式是你想要的那种:
lines=string1.splitlines()
seperator = None
for index, line in enumerate(lines):
if line == "":
seperator = line
elif "Special Permissions" in line:
if seperator != None:
print seperator
print line.lstrip()
offset=0
while True:
#if the line's last 2 characters are ": "
if lines[index+offset][-2:]==": ":
print lines[index+offset+1].lstrip()
offset+=1
else:
break
2
在编程中,有时候我们需要处理一些数据,比如从一个地方获取数据,然后在另一个地方使用这些数据。这个过程就像是把水从一个水桶倒到另一个水桶里。
有些时候,我们会遇到一些问题,比如数据的格式不对,或者数据没有按照我们想要的方式排列。这就像是水桶的形状不一样,导致水倒不进去。
为了避免这些问题,我们可以使用一些工具或方法来确保数据能够顺利地从一个地方转移到另一个地方。这就像是用一个漏斗来帮助我们把水倒得更顺利。
总之,处理数据的时候要注意格式和排列,这样才能让我们的程序运行得更顺利。
# I would replace this with reading lines from a file,
# rather than splitting a big string containing the file.
section = None
inspecialperm = False
with open("testdata.txt") as w:
for line in w:
if not line.startswith(" "):
inspecialperm = False
if section is None:
section = line
elif len(line) == 0:
section = None
elif 'Special Permissions' in line:
if section:
print section
section = ""
inspecialperm = True
print line,
elif inspecialperm:
print line,
0
感谢 milkypostman
、scoffey
和其他人的帮助,我找到了一个解决方案:
def regex():
global text
for paragraph in text.split('\n\n'):
lines = paragraph.split('\n', 1)
#personal modifier to choose certain output only
if lines[0].startswith('C:\\:') or lines[0].startswith('C:\\WINDOWS\system32:') or lines[0].startswith('C:\\WINDOWS\\security:'):
print lines[0]
iterables = re.finditer(r".*Special Permissions: \n(\s+[a-zA-Z ]+\n)*", lines[1])
for items in iterables:
#cosmetic fix
parsedText = re.sub(r"\n$", "", items.group(0))
parsedText = re.sub(r"^\s+", "", parsedText)
parsedText = re.sub(r"\n\s+", "\n", parsedText)
print parsedText
print
我会继续查看所有发布的代码(特别是scoffey的,因为我从来不知道纯字符串操作可以这么强大)。谢谢你们的启发!
当然,这个方法可能不是最优的,但在我的情况下是有效的。如果你有任何建议,欢迎随时分享。
输出结果:
C:\Python27>openfile.py
C:\:
BUILTIN\Users Allowed: Special Permissions:
Create Folders
BUILTIN\Users Allowed: Special Permissions:
Create Files
C:\WINDOWS\security:
BUILTIN\Users Allowed: Special Permissions:
Traverse Folder
Read Attributes
Read Permissions
BUILTIN\Power Users Allowed: Special Permissions:
Traverse Folder
Read Attributes
Read Permissions
C:\WINDOWS\system32:
BUILTIN\Power Users Allowed: Special Permissions:
Delete