Using Regex find/replace in Notepadd++ how can I remove all spaces from a line if the line starts with 'CHAPTER'?
在Notepadd ++中使用Regex查找/替换如果行以'CHAPTER'开头,如何从行中删除所有空格?
Example Text:
CHAPTER A B C
Once upon a time.
What I want to end up with:
我最终想要的是:
CHAPTERABC
Once upon a time.
Incorrect code is something like:
不正确的代码是这样的:
(?<=CHAPTER)( )(?<=\r\n)
So 'CHAPTER' needs to stay and the search should stop at the first line break.
所以'CHAPTER'需要留下来,搜索应该停在第一个换行符。
1 个解决方案
#1
2
You may use a \G
based regex to only match a line that starts with CHAPTER
and then match only consecutive non-whitespace and whitespace chunks up to the linebreak while omitting the matched non-whitespace chunks and removing only the horizontal whitespace:
您可以使用基于\ G的正则表达式仅匹配以CHAPTER开头的行,然后仅匹配连续的非空格和空格块直到换行符,同时省略匹配的非空白块并仅删除水平空格:
(?:^CHAPTER|(?!^)\G)\S*\K\h+
Details:
-
(?:^CHAPTER|(?!^)\G)
-CHAPTER
at the start of a line (^CHAPTER
) or (|
) the end of the previous successful match ((?!^)\G
, as\G
can also match the start of a line, we use the retricting negative lookahead.) -
\S*
- zero or more non-whitespace symbols -
\K
- a match reset operator forcing the regex engine omit the text matched so far (thus, we do not removeCHAPTER
or any of the non-whitespace chunks) -
\h+
- horizontal whitespace (1 or more occurrences) only
(?:^ CHAPTER |(?!^)\ G) - 第一行的开头(^ CHAPTER)或(|)上一次成功匹配的结束((?!^)\ G,如\ G可以也匹配一行的开头,我们使用限制负向前瞻。)
\ S * - 零个或多个非空白符号
\ K - 一个匹配重置操作符,强制正则表达式引擎省略了到目前为止匹配的文本(因此,我们不删除CHAPTER或任何非空白块)
\ h + - 仅水平空格(1次或多次出现)
#1
2
You may use a \G
based regex to only match a line that starts with CHAPTER
and then match only consecutive non-whitespace and whitespace chunks up to the linebreak while omitting the matched non-whitespace chunks and removing only the horizontal whitespace:
您可以使用基于\ G的正则表达式仅匹配以CHAPTER开头的行,然后仅匹配连续的非空格和空格块直到换行符,同时省略匹配的非空白块并仅删除水平空格:
(?:^CHAPTER|(?!^)\G)\S*\K\h+
Details:
-
(?:^CHAPTER|(?!^)\G)
-CHAPTER
at the start of a line (^CHAPTER
) or (|
) the end of the previous successful match ((?!^)\G
, as\G
can also match the start of a line, we use the retricting negative lookahead.) -
\S*
- zero or more non-whitespace symbols -
\K
- a match reset operator forcing the regex engine omit the text matched so far (thus, we do not removeCHAPTER
or any of the non-whitespace chunks) -
\h+
- horizontal whitespace (1 or more occurrences) only
(?:^ CHAPTER |(?!^)\ G) - 第一行的开头(^ CHAPTER)或(|)上一次成功匹配的结束((?!^)\ G,如\ G可以也匹配一行的开头,我们使用限制负向前瞻。)
\ S * - 零个或多个非空白符号
\ K - 一个匹配重置操作符,强制正则表达式引擎省略了到目前为止匹配的文本(因此,我们不删除CHAPTER或任何非空白块)
\ h + - 仅水平空格(1次或多次出现)