[英]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++ 在文本文档的所有行上用
_
字符替换string1
和string2
之间的所有空格。
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 :注意:
\h
with a regular space\h
替换为常规空格\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 string1
或string2
字符序列\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.