简体   繁体   English

多次替换一行上的相同字符 Notepad++

[英]Replace same character on one line multiple times Notepad++

I have a many files and in each file there is a line with the following (amongst other things).我有很多文件,在每个文件中都有一行内容如下(除其他外)。 This line is not always in the same place, however, it starts from the beginning of the line.这条线并不总是在同一个地方,但是,它从线的开头开始。 This line is also always different.这条线也总是不同的。

slug: bláh-téxt-hello-write-sométhing-ábout-arrow

I'd like to replace each occurrence of the special characters (á and é with their corresponding characters a and e).我想用相应的字符 a 和 e 替换每次出现的特殊字符(á 和 é)。 Each of those characters can be on this line many times + also occur in the document in other places (which should not be replaced).这些字符中的每一个都可以在这一行多次出现 + 也出现在文档的其他地方(不应替换)。

So the result is:所以结果是:

slug: blah-text-hello-write-something-about-arrow

I have this: find: ^slug: (. )((é)|(á))(. ) replace: slug: $1(?2e)(?2a)$3我有这个: find: ^slug: (. )((é)|(á))(. ) 替换:slug: $1(?2e)(?2a)$3

However, this seems to replace only one character at a time.但是,这似乎一次只能替换一个字符。 How do I get it to run multiple times until there is no character to replace?如何让它多次运行,直到没有要替换的字符?

Thanks much for any insights.非常感谢您的任何见解。

You can use您可以使用

Find What : (?:\\G(?!^)|^slug:\\h*)[^\\r\\náé]*\\K(?:(á)|é)查找内容: (?:\\G(?!^)|^slug:\\h*)[^\\r\\náé]*\\K(?:(á)|é)
Or或者
Find What : (?:\\G(?!^)|^slug:\\h*).*?\\K(?:(á)|é)查找内容: (?:\\G(?!^)|^slug:\\h*).*?\\K(?:(á)|é)
Replace With : (?1a:e)替换为:( (?1a:e)

Details :详情

  • (?:\\G(?!^)|^slug:\\h*) - end of the previous match or slug: and then zero or more horizontal whitespaces at the start of a line (?:\\G(?!^)|^slug:\\h*) - 前一个匹配或slug:结尾,然后在一行的开头有零个或多个水平空格
  • [^\\r\\náé]* - zero or more chars other than CR, LF, á and é [^\\r\\náé]* - 除 CR、LF、 áé之外的零个或多个字符
  • .*? - will match zero or more chars other than line break chars, as few as possible - 将匹配除换行符以外的零个或多个字符,尽可能少
  • \\K - the operator that discards all text matched so far \\K - 丢弃到目前为止匹配的所有文本的运算符
  • (?:(á)|é) - either á (captured into Group 1) or é . (?:(á)|é) - á (捕获到组 1)或é

In the replacement, (?1a:e) , replaces the found match with a if Group 1 matched, else, e is used.在替换中, (?1a:e)将找到的匹配替换a如果组 1 匹配,否则使用e

See the regex demo.请参阅正则表达式演示。

在此处输入图片说明

Extra information about the use of conditional replacement is available in my " Swapping multiple values using conditional replacement patterns in Notepad++ " YT video.我的“在 Notepad++ 中使用条件替换模式交换多个值” YT 视频中提供了有关使用条件替换的额外信息。

Extra information about the use of \\G operator can be found in another YT video of mine, " \\G anchor use cases ".关于\\G运算符使用的额外信息可以在我的另一个 YT 视频“ \\G 锚点用例”中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM