0

我需要帮助来创建 4 个正则表达式模式来匹配文本块中的 4 个不同的东西。

这是文本块(名称无关紧要):

[2013/8/27 - 20:44:3] AdmCmd: Damian Santos was fined $1000 by Scott Parker, reason: BH(8-27-2013)
[2013/8/29 - 21:56:49] AdmCmd: Jimmy Greiner was fined $304389 by Scott Parker, reason: LOL(8-29-2013)
[2013/8/29 - 22:4:54] AdmCmd: Jimmy Greiner has been jailed by Scott Parker, reason: LOL(8-29-2013)
[2013/8/29 - 22:5:38] AdmCmd: Jimmy Greiner has been released from prison by Scott Parker, reason: TS(8-29-2013)
[2013/8/29 - 22:42:55] AdmCmd: James McAdams has been released from prison by Damian Santos, reason: test(8-29-2013)
[2013/8/30 - 20:17:23] AdmCmd: James McAdams has been released from prison by Scott Parker, reason: TS(8-30-2013)
[2013/8/30 - 20:26:15] AdmCmd: James McAdams has been jailed by Jimmy Greiner, reason: Test(8-30-2013)
[2013/8/30 - 20:26:37] AdmCmd: James McAdams has been released from prison by Scott Parker, reason: Test(8-30-2013)
[2013/8/31 - 11:35:35] AdmCmd: Scott Parker was fined $5000 by Scott Parker, reason: BH(8-31-2013)
[2013/8/31 - 11:36:0] AdmCmd: Scott Parker was fined $5000 by Scott Parker, reason: BH(8-31-2013)
[2013/8/31 - 11:36:49] AdmCmd: Damian Santos was fined $5000 by Scott Parker, reason: UNRP driving(8-31-2013)

我需要匹配以下姓名:罚款释放入狱。我已经有了这个模式:

[A-Za-z ]+(?=was|has)

我需要发出这些行动之一的人的姓名(每个句子末尾附近的姓名)。我试图为它找到一个模式,但我不能排除“by”这个词:

(?=by)[A-Za-z ]+(?=,)

由于原因(没有日期)和日期,我没有尝试模式,它们都位于末尾。我需要通过漂亮的图案将它们切割干净。

我会很感激一些帮助。

谢谢你。

4

2 回答 2

0

我试图为它找到一个模式,但我不能排除“by”这个词:

使用积极的lookbehining而不是积极的lookahead:

(?<=by )[A-Za-z ]+(?=,)

工作演示

于 2013-09-10T18:00:22.573 回答
0

要排除“by”,您需要使用积极的lookbehind,因此将您的更改(?=by)(?<=by).

出于这个原因,您可以使用相同的方法,(?<=reason:)然后使用正则表达式来匹配 BH 或任何预期的代码。

最后但并非最不重要的一点是,您应该不难捕捉到行尾括号“之间”的日期。您似乎走在正确的轨道上,完成它应该没有问题!

于 2013-09-10T18:01:53.957 回答