我正试图弄清楚如何对日志进行排序......
我希望输出文本也是这里有标签空间。
用户:等级:域:时间:布拉赫:日期:IP
答案 0 :(得分:0)
如果我的问题是正确的,那么你所说的不是排序,而是解析。您有要转换为其他格式的日志字符串。匹配日志字符串的正则表达式为
(?P<User>[^:]+):(?P<Level>[^:]+):(?P<Domain>[^:]+):(?P<Time>[^:]+):(?P<Blah>[^:]+):(?P<Date>[^:]+):(?P<IP>[^:]+)
但是,由于你有这么多组,所以可以更有效地完成,这是python中的一个例子
import re
logString = "User:Level:Domain:Time:Blah:Date:IP"
logGroups = ["User", "Level", "Domain", "Time", "Blah", "Date", "IP"]
reLogGroups = "(?P<"+">[^:]+):(?P<".join(logGroups)+">[^:]+)"
matchLogGroups = re.search(reLogGroups,logString)
if matchLogGroups:
counter = 1
for logGroup in logGroups:
print(str(counter)+". " + logGroup + ": " + matchLogGroups.group(logGroup) + "\n")
counter += 1
输出
1. User: User
2. Level: Level
3. Domain: Domain
4. Time: Time
5. Blah: Blah
6. Date: Date
7. IP: IP