a_todd12
a_todd12

Reputation: 550

Advanced find and replace in Notepad++

I'm trying to use Notepad++ for the first time and wanted to see if anyone here had some solutions for me.

I have a document that looks like this:

ex 1 
send power g1 g2 g3 g4 
ex 2 
send power g1 g2 
ex 3 
send power g1 f5 f3
ex 4 
send f5 f2 t5

... and so on, with various combinations of words after 'send'. I just need to add a line (" this is number `#'") after each 'send' line, with the # matching the 'ex #' number...so it would increase by 1 each time. The result would be:

ex 1 
send power g1 g2 g3 g4
this is number `1' 
ex 2
send power g1 g2 
this is number `2'
ex 3 
send power g1 f5 f3
this is number `3'
ex 4 
send f5 f2 t5
this is number `4'

Any advice would be greatly appreciated!

Upvotes: 1

Views: 570

Answers (3)

The fourth bird
The fourth bird

Reputation: 163217

In Notepad++ you can:

Find what:

\bex\h+(\d+)\h*\R.*\K

Replace with:

\r\nthis is number `$1`

The pattern matches:

  • \bex A word boundary, match the word ex
  • \h+ Match 1+ horizontal whitespace chars
  • (\d+) Capture group 1, match 1+ digits
  • \h* Match optional horizontal whitespace chars
  • \R Match any unicode newline sequence
  • .*\K Match the whole line and forget what is matched so far

Regex demo

enter image description here

Upvotes: 2

CertainPerformance
CertainPerformance

Reputation: 370679

Capture the number after the ex, then substitute in with the newline, this is number, and the number.

Match

ex (\d+) *\r?\n.+

and replace with

$0\nthis is number `$1`

(\d+) captures the digits. $0 substitutes with the whole matched string. $1 substitutes with the value in the first capture group (the digits).

Upvotes: 2

chiliNUT
chiliNUT

Reputation: 19573

find:

ex (\d+)\s+^.*$

replace:

$0\r\nthis is number `$1`

searches for ex followed by a number followed by 1 or more spaces and/or line breaks followed by the entire next line

replaces with: $0 the entire thing it found, a newline, this is number, and the number after "ex"

Upvotes: 2

Related Questions