Notepad++ v8.5.4 替换功能无法正常工作的字符串

huangapple go评论55阅读模式
英文:

Notepad++ v8.5.4 Replace not working for these strings

问题

我正在处理这样的行

[6268:1434][2023-07-17T03:44:08]i001: Burn v3.14.0.4118,Windows v10.0(Build 22631:Service Pack 0),路径:C:\Users\david\AppData\Local\Temp{95942D14-E274-486C-A7FB-EDAF9E773422}.cr\winsdksetup.exe

[6268:1434][2023-07-17T03:44:08]i009: 命令行:'-burn.clean.room=E:\Downloads\winsdksetup.exe -burn.filehandle.attached=728 -burn.filehandle.self=740'

对于搜索,我使用

"(.)2023-07-17(.): "

双引号只是显示正在使用的字符串,并选择正则表达式。
这个方法有效。

如果我想要将其替换为两个空格字符。

应该是

Burn v3.14.0.4118, Windows v10.0(Build 22631:Service Pack 0),路径:C:\Users\david\AppData\Local\Temp{95942D14-E274-486C-A7FB-EDAF9E773422}.cr\winsdksetup.exe

获得

C:\Users\david\AppData\Local\Temp{95942D14-E274-486C-A7FB-EDAF9E773422}.cr\winsdksetup.exe

应该是

命令行:'-burn.clean.room=E:\Downloads\winsdksetup.exe -burn.filehandle.attached=728 -burn.filehandle.self=740'

获得

'-burn.clean.room=E:\Downloads\winsdksetup.exe -burn.filehandle.attached=728 -burn.filehandle.self=740'

为什么第一个": "没有被用作搜索定界符,而是最后一个?

英文:

I am working with lines like this

[6268:1434][2023-07-17T03:44:08]i001: Burn v3.14.0.4118, Windows v10.0 (Build 22631: Service Pack 0), path: C:\Users\david\AppData\Local\Temp\{95942D14-E274-486C-A7FB-EDAF9E773422}\.cr\winsdksetup.exe

[6268:1434][2023-07-17T03:44:08]i009: Command Line: '-burn.clean.room=E:\Downloads\winsdksetup.exe -burn.filehandle.attached=728 -burn.filehandle.self=740'

For search I use

    "(.*)2023-07-17(.*): "    

The Double Quotes just show the string being used and with Regular Expression selected.
That works.

If I then want to replace that with two blank characters.

Should be

Burn v3.14.0.4118, Windows v10.0 (Build 22631: Service Pack 0), path: C:\Users\david\AppData\Local\Temp\{95942D14-E274-486C-A7FB-EDAF9E773422}\.cr\winsdksetup.exe

getting

C:\Users\david\AppData\Local\Temp\{95942D14-E274-486C-A7FB-EDAF9E773422}\.cr\winsdksetup.exe

Should be

Command Line: '-burn.clean.room=E:\Downloads\winsdksetup.exe -burn.filehandle.attached=728 -burn.filehandle.self=740'

getting

'-burn.clean.room=E:\Downloads\winsdksetup.exe -burn.filehandle.attached=728 -burn.filehandle.self=740'

Why is the first ": " not being used as the search delimiter instead of the Last?

答案1

得分: 0

为什么第一个 ":" 没有被用作搜索分隔符,而是最后一个?

这是因为 .* 正则表达式匹配允许其余正则表达式匹配的最长序列。也就是说,默认情况下,.* 是“贪婪”的。您有两个选择:

  1. 切换到非贪婪匹配:.*?。请参阅 https://www.boost.org/doc/libs/1_80_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

  2. 将第二个 .* 改为匹配最大数量的非冒号字符:[^:]*

英文:

> Why is the first ": " not being used as the search delimiter instead
> of the Last?

That is because the .* regex matches the longest sequence that allows the rest of the regex to match. That is, .* is "greedy" by default. You have two options:

  1. Switch to a non-greedy match: .*?. See https://www.boost.org/doc/libs/1_80_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html.

  2. Change the second .* to match the maximal number of non-colon characters: [^:]*.

答案2

得分: 0

  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • 查找内容:^.+?2023-07-17.\d\d:\d\d:\d\d\][^:]+:
  • 替换为:LEAVE EMPTY
  • 勾选 环绕
  • 选择 正则表达式
  • 取消勾选 . 匹配换行符
  • <kbd>全部替换</kbd>
英文:
  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • Find what: ^.+?2023-07-17.\d\d:\d\d:\d\d\][^:]+:
  • Replace with: LEAVE EMPTY
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • <kbd>Replace all</kbd>

Explanation:

^               # beginning of line
.+?             # 1 or more any character but newline, not greedy
2023-07-17      # literally
.               # 1 any character
\d\d:\d\d:\d\d  # 2 digits, colon, 2 digits, colon, 2 digit
\]              # ] character
[^:]+           # 1 or more any character that is not a colon
:               # colon and sapce

Screenshot (before):

Notepad++ v8.5.4 替换功能无法正常工作的字符串

Screenshot (after):

Notepad++ v8.5.4 替换功能无法正常工作的字符串

huangapple
  • 本文由 发表于 2023年7月20日 09:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定