将文本添加到捕获组内的每一行

将文本添加到捕获组内的每一行

我拥有的:

X
(
1
2
3
)
BREAK

Y
(
4
5
)
BREAK

Z
(
6
7
8
9
)
BREAK

我想要的是:

X,1
X,2
X,3

Y,4
Y,5

Z,6
Z,7
Z,8
Z,9

我更愿意弄清楚如何使用 Notepad++ 中的 RegEx 来执行此操作。据我了解,我需要在“查找”文本框中输入类似以下内容:

^(.*?)\((.*?)\).*?BREAK

Search Mode存在Regular Expression并被. matches newline选中。

但我不知道在“查找/替换”输入框的“替换”部分中输入什么。

\r\n我如何指定要用之前捕获的字符(XY或)替换两个括号之间的每个实例Z

答案1

我假设您的XYZ是单词,123是数字。如果不是这样,请将下面的内容替换\w+为您需要的任何字符类,并将下面的内容替换\d+为您需要的任何字符类。


  • Ctrl+H
  • 找什么:^(\w+)\R\(\R(\d+\R)(?:(\d+\R))?(?:(\d+\R))?(?:(\d+\R))?(?:(\d+\R))?\)\RBREAK
  • 用。。。来代替:$1,$2(?3$1,$3)(?4$1,$4)(?5$1,$5)(?6$1,$6)
  • 查看 环绕
  • 查看 正则表达式
  • Replace all

解释:

^               # beginning of line
(\w+)           # group 1, 1 or more word character, you can use the character class you need
\R              # any kind of linebreak
\(              # an opening parenthesis
\R              # any kind of linebreak
(               # start group 2
  \d+           # 1 or more digits, you can use the character class you need
  \R            # any kind of linebreak
)               # end group 2
(?:             # non capture group
    (\d+\R)     # group 3, same pattern as group 2
)?              # end non capture group, optional
(?:(\d+\R))?    # same as above, group 4
(?:(\d+\R))?    # same as above, group 5
(?:(\d+\R))?    # same as above, group 6
            You can add more structures as above depending on max number of lines you have.
\)              # closing parenthesis
\R              # any kind of linebreak
BREAK           # literally

替代品:

$1,$2           # content of group 1, comma, content of group 2
(?3             # if group 3 exists then:
    $1,$3         # content of group 1, comma, content of group 3
)               # endif
(?4$1,$4)       # same as above for group 4
(?5$1,$5)       # same as above for group 5
(?6$1,$6)       # same as above for group 6
            Add as many similar structures as above depending on max number of lines you have.

截图(之前):

在此处输入图片描述

截图(之后):

在此处输入图片描述

相关内容