1

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!

3 Answers 3

2

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"

2

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).

2
  • Thanks, I follow the logic here but for some reason it's seemingly replacing everything with blank lines. Maybe I typed something in wrong? Does it matter if my 'ex 1' line actually has two words? The full line is really "example solution 1" but I just wrote 'ex' here to save space.
    – a_todd12
    Commented Jul 16, 2022 at 20:35
  • 1
    I think we posted the same idea at the same time, except I assumed \r\n for line breaks b/c notepad++ is windows only
    – chiliNUT
    Commented Jul 16, 2022 at 20:52
2

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

4
  • I'd capture the \R in order to reuse it in the replacement to have homogeneus linebreaks.
    – Toto
    Commented Jul 17, 2022 at 13:05
  • @Toto I see your point, only I think that if there is no newline after the last line like in send f5 f2 t5 there is no newline to capture to use in the replacement. Or did you mean something different? Commented Jul 17, 2022 at 15:01
  • 1
    You're rigth, I haven't thought this case !
    – Toto
    Commented Jul 17, 2022 at 15:16
  • After reflexion, there is allways a linebreak after the line ex n
    – Toto
    Commented Jul 18, 2022 at 9:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.