简体   繁体   English

替换两个字符串之间所有出现的字符

[英]Replace all occurrences of a character between two strings

I want to replace all space occurrences between string1 and string2 with a _ character on all lines of a text document using Notepad++.我想使用 Notepad++ 在文本文档的所有行上用_字符替换string1string2之间的所有空格。

Examples:例子:

string1 this is a first example string2
string1 this is a second example string2

Expected result:预期结果:

string1_this_is_a_first_example_string2
string1_this_is_a_second_example_string2

I tried this expression (?<=string1)(\s*)(?=string2) , but it didn't work.我试过这个表达式(?<=string1)(\s*)(?=string2) ,但它没有用。

You may use您可以使用

Find What : (?:\G(??^)|string1)(:?(.?string1|string2)?)*.\K\h(?=.*string2)查找内容(?:\G(??^)|string1)(:?(.?string1|string2)?)*.\K\h(?=.*string2)
Replace With : _替换为_

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

NOTE :注意

  • If you need to only match regular spaces, replace \h with a regular space如果您只需要匹配常规空格,请将\h替换为常规空格
  • A more appropriate check for the end of the previous successful match is \G(??^(?<![\s\S])) , but if your expected matches are on a single line, you might go on using \G(?!^) .对上一个成功匹配结束的更合适的检查是\G(??^(?<![\s\S])) ,但如果您的预期匹配在一行上,您可能会 go 使用\G(?!^)

Details细节

  • (?:\G(?!^)|string1) - either the end of the previous match (but not start of a line) or string1 (?:\G(?!^)|string1) - 前一个匹配的结尾(但不是行首)或string1
  • (?:(?.string1|string2)?)*? - any char, 0 or more times, but as few as possible, that is not starting string1 or string2 char sequence - 任何字符,0 次或更多次,但尽可能少,即不是开始string1string2字符序列
  • \K - discard the text matched so far \K - 丢弃目前匹配的文本
  • \h - any horizontal whitespace \h - 任何水平空格
  • (?=.*string2) - there must be a string2 after any 0 or more chars other than line break chars as many as possible immediately to the right of the current location. (?=.*string2) - 在当前位置右侧的任何 0 个或多个除换行符之外的字符之后必须有一个string2

在此处输入图像描述

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

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