Q:

Prepending Text To Every Line Within Capture Group

What I have:
X
(
1
2
3
)
BREAK

Y
(
4
5
)
BREAK

Z
(
6
7
8
9
)
BREAK

What I'd like:
X,1
X,2
X,3

Y,4
Y,5

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

I'd prefer to figure out how to do this using RegEx in Notepad++.
As I understand it, I'd need something like this in the Find text box:
^(.*?)\((.*?)\).*?BREAK

with Search Mode being Regular Expression and . matches newline selected.
But I don't know what to enter in the Replace section of the Find/Replace input box.
How can I specify that I want to replace every instance of \r\n between the two brackets with the previously captured character (X, Y or Z)?

A:

I assume yours X, Y, Z are words and 1, 2, 3 are numbers. If it is not the case, replace below \w+ with any character class you need, and \d+ with any character class you need.

Ctrl+H
Find what: ^(\w+)\R\(\R(\d+\R)(?:(\d+\R))?(?:(\d+\R))?(?:(\d+\R))?(?:(\d+\R))?\)\RBREAK
Replace with: $1,$2(?3$1,$3)(?4$1,$4)(?5$1,$5)(?6$1,$6)
CHECK Wrap around
CHECK Regular expression
Replace all

Explanation:
^               # 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

Replacement:
$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.

Screenshot (before):

Screenshot (after):

