字符串出现前的正则表达式匹配 [英] Regex match before string comes up

查看:38
本文介绍了字符串出现前的正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个包含来自游戏服务器的 10,000 多行消息的文件,如下所示:

So i have this file of 10,000+ lines of messages from a game server, like so:

11.07.23 08:40:16 [INFO] NC:移动违规:来自 yasmp 的 wolfman98(-90.8, 64.0, 167.5) 到 (-90.7, 64.0, 167.3) 距离 (0.0, 0.0, 0.2)

11.07.23 08:40:16 [INFO] NC: Moving violation: wolfman98 from yasmp (-90.8, 64.0, 167.5) to (-90.7, 64.0, 167.3) distance (0.0, 0.0, 0.2)

11.07.23 10:57:44 [INFO] NC:移动违规:来自 yasmp 的 AKxiZeroDark(-1228.3, 11.2, 1098.7) 到 (-1228.3, 11.2, 1098.7) 距离 (0.0, 0.0,0.0)

11.07.23 10:57:44 [INFO] NC: Moving violation: AKxiZeroDark from yasmp (-1228.3, 11.2, 1098.7) to (-1228.3, 11.2, 1098.7) distance (0.0, 0.0, 0.0)

我拥有的当前正则表达式代码是:\d{1,4}\.\d{1},它匹配到目前为止所有以粗体显示的内容:

The current regex code i have is: \d{1,4}\.\d{1}, which matches so far everything in bold:

11.07.23 08:40:16 [INFO] NC:移动违规:来自 yasmp 的 wolfman98(-90.864.0167.5) 到 (-90.7, 64.0, 167.3) 距离(0.0, <强>0.0, 0.2)

11.07.23 08:40:16 [INFO] NC: Moving violation: wolfman98 from yasmp (-90.8, 64.0, 167.5) to (-90.7, 64.0, 167.3) distance (0.0, 0.0, 0.2)

我一直无法找到一种方法来获取仅显示以下内容的部分:

Ive been having trouble finding a way to get the part that only says:

(-1228.3, 11.2, 1098.7) 到 (-1228.3, 11.2, 1098.7)

(-1228.3, 11.2, 1098.7) to (-1228.3, 11.2, 1098.7)

在距离"之前word,并且在开始时没有时间戳,最终将其替换为这样的结尾:

before the "distance" word, and without the timestamp in the beginning, and eventually replacing it to end up like this:

11.07.23 08:40:16 [INFO] NC:移动违规:来自 yasmp 的 wolfman98(-#, #, #)(-#, #, #) 距离 (0.0, 0.0, 0.2)

11.07.23 08:40:16 [INFO] NC: Moving violation: wolfman98 from yasmp (-#, #, #) to (-#, #, #) distance (0.0, 0.0, 0.2)

11.07.23 10:57:44 [INFO] NC:移动违规:来自 yasmp 的 AKxiZeroDark(-#, #, #)(-#, #, #) 距离 (0.0, 0.0, 0.0)

11.07.23 10:57:44 [INFO] NC: Moving violation: AKxiZeroDark from yasmp (-#, #, #) to (-#, #, #) distance (0.0, 0.0, 0.0)

还有一些额外的信息,数字可以是负数也可以不是负数,范围从 1.0 位到 1234.0 位,这就是为什么我需要在单词距离"之前帮助匹配.再次.

And a bit of extra information, the numbers can be either negative or not, ranging from 1.0 digit to 1234.0 digits, which is why i need help matching before the word "distance" again.

甚至,如果整个事情没有出现也没关系:

Or even, it would be fine if the entire thing didnt show up:

11.07.23 08:40:16 [INFO] NC:移动违规:来自 yasmp 的 wolfman98距离 (0.0, 0.0, 0.2)

11.07.23 08:40:16 [INFO] NC: Moving violation: wolfman98 from yasmp distance (0.0, 0.0, 0.2)

11.07.23 10:57:44 [INFO] NC:移动违规:来自 yasmp 的 AKxiZeroDark距离 (0.0, 0.0, 0.0)

11.07.23 10:57:44 [INFO] NC: Moving violation: AKxiZeroDark from yasmp distance (0.0, 0.0, 0.0)

推荐答案

扩展您的数字匹配正则表达式的一个相当多毛的正则表达式是 \((?:-?\d{1,4}\.\d{1}(?:, |\))){3} 到 \((?:-?\d{1,4}\.\d{1}(?:, |\))){3}(?=距离).让我们稍微分解一下.

A fairly hairy looking regex that extends your number matching regex would be \((?:-?\d{1,4}\.\d{1}(?:, |\))){3} to \((?:-?\d{1,4}\.\d{1}(?:, |\))){3}(?= distance). Let's break that down a little.

由两组相同的组组成,以匹配括号中的两组数字:\((?:-?\d{1,4}\.\d{1}(?:, |\))){3}.正则表达式现在允许在数字之前使用可选的 -,这使得数字匹配 -?\d{1,4}\.\d{1}.在每个数字之后有一个逗号或一个括号,因此为了迭代数字匹配,我们也需要它:(?:, |\)).然后整个野兽以 \( 为前缀以获得数字组的开头括号.该正则表达式重复两次以获得 to 匹配的两组数字-之间.

It is made up of two groups that are identical to match the two groups of numbers in parens: \((?:-?\d{1,4}\.\d{1}(?:, |\))){3}. The regex now allows an optional - before the number and which makes the number match -?\d{1,4}\.\d{1}. After each number there is either a comma or a paren, so to iterate the number match we need that as well: (?:, |\)). That entire beast is then prefixed with \( to get the opening paren of the number group. That regex is repeated twice to get the two groups of numbers with the to match in-between.

最后一位是正向预测,以确保我们匹配后面跟有单词 distance 的数字组.该词不会包含在匹配中,但必须存在于正则表达式中才能匹配.

The final bit is a positive look-ahead to ensure that we are matching the number groups that are followed by the word distance. That word will not be included in the match, but will have to be there for the regex to match.

我使用了非捕获组((?: ... ) 的东西),因为我不知道你想用捕获做什么.

I've used non-capturing groups (the (?: ... ) stuff) because I don't know what you want to do with the captures.

我已经使用 perl 5.12.2 对您的两个示例日志文件行进行了尝试,它似乎有效.

I've tried this out against your two example logfile lines using perl 5.12.2 and it seems to work.

这篇关于字符串出现前的正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆