python正则表达式查找和哈希用户名

python正则表达式查找和哈希用户名,python,regex,Python,Regex,我想在我的日志文件中散列用户名——我的正则表达式不能正常工作 输入示例: Account Name: - Account Domain: - ImportantStuff Account Name: Foo bar Account Domain: my.bar Account Name: Foo-bar Supplied Realm Name: my.bar ImportantStuff Account Name: Foo99bar$ Account Domain: my.ba

我想在我的日志文件中
散列用户名
——我的正则表达式不能正常工作

输入示例:

Account Name:  -  Account Domain: - ImportantStuff Account Name:  Foo bar  Account Domain: my.bar
Account Name:  Foo-bar  Supplied Realm Name: my.bar ImportantStuff 
Account Name:  Foo99bar$  Account Domain: my.bar ImportantStuff Account Name:  -  Account Domain: -
我的正则表达式:

(((?!Account Name:\s+-\s+))(Account Name:\s+(\S+.+(?=\s+Account))))|(Account Name:\s+(\S+.+(?=\s+Supplied)))((?!Account Name:\s+-\s+))
我想筛选为:
  • 如果模式为“帐户名:-”忽略

  • 如果模式不是“帐户名:-”,则获取用户名

我无法将“-”作为分隔符进行筛选,因为某些用户名包含“-”,这就是我使用\s-\s(?!忽略模式)的原因。空白也是如此

之后,用户名将被哈希:

result2 = re.sub(r'(((?!Account Name:\s+-\s+))(Account Name:\s+(\S+.+(?=\s+Account))))|(Account Name:\s+(\S+.+(?=\s+Supplied)))((?!Account Name:\s+-\s+))', lambda m: m.group(1) + hashlib.sha512(m.group(2)).hexdigest(), line)
起初我尝试[^帐户名:\s+-\s+],但当然,随后所有内容都会匹配,这不在[^]范围内,也不会被视为字符串

我可以这样做吗

((?!Account Name: - )|Account Name:\s+(.+?(?=\s+Account Domain|Supplied)))

我正在运行
python2.7

最好先把它分解成多个问题

因为您的日志具有相同的结构(这里我假设您的用户名没有空格)。所以先按块分割

然后,您的用户名始终位于特定块上


在此特定块上,您甚至可以使用更简单的正则表达式应用任何规则。

如果
帐户名:
后跟空格,则所有匹配都可能失败,使用
(?!\s+-\s)
负前瞻:

(Account Name:(?!\s+-\s)\s*)(.*?)(?=\s+(?:Account Domain|Supplied))

详细信息

  • (帐户名:(?!\s+-\s)\s*)
    -第1组:
    帐户名:
    ,后面不紧跟1+空格、
    -
    和一个空格(
    (?!\s+-\s)
    ),然后是0+空格
  • (.*)
    -第2组:除换行符以外的任何零个或多个字符,尽可能少
  • (?=\s+(?:帐户域|提供的))
    -正向前瞻,要求1+个空格,后跟当前位置右侧的
    帐户域
    提供的
    子字符串
见:

输出:

Account Name:  -  Account Domain: - ImportantStuff Account Name:  45a19ebf5c5c04bf71e9819b29e9a71ee7b4f9b5d3de72615b9788da05eceb526cc47b18e108107a3e53ee2068c4da4fca8209e9e2d87560d6848823eebe803b  Account Domain: my.bar
Account Name:  4ac1e08061b7216e9d3e0a44d6ca6512a25577a1e0675ba7cb439fc243e84d566dd0c1aac33f89c5c23e959fef5dc6a71cdd2adba257c81975caa822be4e5018Supplied Realm Name: my.bar ImportantStuff
Account Name:  7228cb36d1d3b5cd41d50d150defd13e06441eb2b6a4689f9356012607fb0ebf5680af49f743baf289a590a07f8da6077f5288a5d4000448bfc7fd303869d31f  Account Domain: my.bar ImportantStuff Account Name:  -  Account Domain: -

我的用户名包含空格,而且它们不在同一个位置。看看我的输入示例。一些日志消息有两个“Account Name:”-字段,但只有一个包含用户名,另一个为空(-),这正是我想做的-非常感谢
Account Name:  -  Account Domain: - ImportantStuff Account Name:  45a19ebf5c5c04bf71e9819b29e9a71ee7b4f9b5d3de72615b9788da05eceb526cc47b18e108107a3e53ee2068c4da4fca8209e9e2d87560d6848823eebe803b  Account Domain: my.bar
Account Name:  4ac1e08061b7216e9d3e0a44d6ca6512a25577a1e0675ba7cb439fc243e84d566dd0c1aac33f89c5c23e959fef5dc6a71cdd2adba257c81975caa822be4e5018Supplied Realm Name: my.bar ImportantStuff
Account Name:  7228cb36d1d3b5cd41d50d150defd13e06441eb2b6a4689f9356012607fb0ebf5680af49f743baf289a590a07f8da6077f5288a5d4000448bfc7fd303869d31f  Account Domain: my.bar ImportantStuff Account Name:  -  Account Domain: -