如何使用正则表达式搜索和替换重新排列文本行?

问题描述 投票:0回答:1

我有一个日志文件,其中包含 6 年前我们服务的时间戳和用户名。问题在于,新系统不会接受 3 年前的任何未经大量编辑的日志。

项目的格式如下:

[2018/01/21 13:51:23] UsernameHere: I sent this message, which doesn't affect the ability to input the contents into the system.

我需要将它们设置为如下格式:

UsernameHere — 2018/01/21 1:51 PM
I sent this message, which doesn't affect the ability to input the contents into the system.

我尝试使用正则表达式匹配功能来匹配:

\[2018\-[01][0-9]\-[0-3][0-9] [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\] UsernameHere

并替换为:

UsernameHere — 2018\-[01][0-9]\-[0-3][0-9] [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9](\r)(\n)

但是这样做会导致文本完全按照替换栏的说明输出,而不是反向应用正则表达式并保持日期部分内的原始文本完好无损。

任何将名称移到前面的帮助将不胜感激。一旦第一个问题得到解决,我可能会单独发帖请求有关时间戳详细信息(24 小时到 12 小时)的帮助。

regex notepad++
1个回答
0
投票

之前:

这是匹配每行第三次出现的“:”的正则表达式模式:

^(([^:]*:){2}[^:]*):\s*

然后在“替换为”字段中用换行符代替“:”:

\1\r\n

之后:

最后替换方括号。

© www.soinside.com 2019 - 2024. All rights reserved.