anon
anon

Reputation: 560

Inserting regex match on next line

I have a file like

[A()]
[B(), C()]

I want to move C() onto the line below it like so

[A()]
[B()]
[C()]

Right now, I have \,?\s?C\(.*\) to match the C() part. I'm not sure how to insert the match onto the next line though.

Once that happens it will probably look like this in most cases:

[A()]
[B()]
, C()

And that's fine, I will need to do another pass to pre/append the brackets and remove the comma. My question is only about inserting the matched capture group onto the next line (and removing it from its current line).

If there was some token to act as the line the match was found (without the match) known as $$ for example, I would want to replace \,?\s?C\(.*\) with $$\n$1, assuming $1 is the matched text (then do another pass for the aforementioned reasons).

I'm in Notepad++.

Upvotes: 0

Views: 220

Answers (2)

Toto
Toto

Reputation: 91385

  • Ctrl+H
  • Find what: ,\h*(C\()
  • Replace with: ]\n[$1 or ]\r\n[$1 depending on your needs
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

,       : a comma
\h*     : 0 or more horizontal spaces
(       : start group 1
  C\(   : C followed by an open parenthesis
)       : end group

Replacement:

]       : close bracket
\n      : linefeed
[       : open bracket
$1      : content of group 1, ie. C(

Result for given example:

[A()]
[B()]
[C()]

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370679

To do it all in one go, you can search for

\,?\s?C(\(.*?\))

and replace with

]\n[C\1

That is - match the leading comma and whitespace (if it exists), capture the argument list and its surrounding parentheses, then replace with ending bracket, newline, and that captured group

https://regex101.com/r/tBHaNU/1

Upvotes: 1

Related Questions