使用通配符在记事本++中查找/替换字符

使用通配符在记事本++中查找/替换字符

我已经对这个主题进行了一些研究,但还没有找到适合我特定需求的确切解决方案。我正在使用 Notepad++ 编写一些 Z80 汇编代码。我需要将所有“:”字符替换为“::”,但仅限于特定情况。例如,假设我有如下代码:

SomeFunction:
  ld a, whatever
  ret

; Remember to do this: blah blah blah

Function2:
  jr z, someWhere

.subfunction:
  ld hl, somePointer
  ret

通常情况下,我可以继续使用 Ctrl+H 将所有“:”替换为“::”……但是,在这个特殊情况下,我想避免替换以“.”或“;”开头的行上的单个“:”字符。所有其他“:”实例,都改为“::”。本质上,最终结果将如下所示:

SomeFunction::
  ld a, whatever
  ret

; Remember to do this: blah blah blah

Function2::
  jr z, someWhere

.subfunction:
  ld hl, somePointer
  ret

我说得有道理吗?我知道我可以(也应该)使用正则表达式,而且会用到通配符,但我不确定如何我会对其进行格式化。非常感谢!

答案1

您可以尝试以下操作:

  • 找什么:^(?=[^\;|\.])(.+\:)$
  • 替换为:``
  • 搜索模式:正则表达式

答案2

这还可以检查冒号是否已经加倍。

  • Ctrl+H
  • 找什么:^(?![;.]).*?(?<!:):\K(?!:)
  • 用。。。来代替::
  • 查看 环绕
  • 查看 正则表达式
  • 取消选中 . matches newline
  • Replace all

解释:

^           # beginning of line
    (?![;.])    # negative lookahead, make sure the next character is not ; or .
    .*?         # 0 or more any character but newline, not greedy
    (?<!:)      # negative lookbehind, make sure the previous character is not :
    :           # a colon
    \K          # forget all we have seen until this position
    (?!:)       # negative lookahead, make sure the next character is not :

截图(之前):

在此处输入图片描述

截图(之后):

在此处输入图片描述

相关内容