0

Given the following file :

Chapter 1
1 line 1
2 line 2
3 line 3

Chapter 2
1 line 4
2 line 5

I would like to add the chapter number on each line number :

Chapter 1
1-1 line1
1-2 line 2
…
Chapter 2
2-1 line 4
…

Is it possible using regular expressions ? Thanks for any insight.

  • Possible duplicate of [Notepad++ Regular Expression add up numbers](http://stackoverflow.com/questions/20506990/notepad-regular-expression-add-up-numbers) – ssc-hrep3 Jan 27 '17 at 16:10
  • It can be done with a regex in Find Replace dialog, just you would need to click Replace all until no match is found. I have already answered a similar question, but cannot look fIr it now. – Wiktor Stribiżew Jan 27 '17 at 16:23
  • @WiktorStribiżew It can't be done in 1 shot can it? – Mohammad Yusuf Jan 27 '17 at 16:26
  • @MYGz I can't say for sure but it probably can be done with one regex but multiple replace all hits. I am on a mobile now so I cannot check. – Wiktor Stribiżew Jan 27 '17 at 16:33
  • Try `^Chapter\h+(\d+).*\R(\1-\d.*\R)*+\K(?!\1-\d)\d+` regex and `$1-$&` replacement. Click *Replace all* until no match is found (until 0 occuurences are replaced). – Wiktor Stribiżew Jan 29 '17 at 10:07

1 Answers1

0

Use

Find What: ^Chapter\h+(\d+).*\R(\1-\d.*\R)*+\K(?!\1-\d)\d+
Replace With: $1-$&

Hit Replace All until no replacement takes place.

Details:

  • ^ - start of a line
  • Chapter - a literla char sequence
  • \h+ - 1 or more horizontal whitespaces
  • (\d+) - Capturing group 1 matching one or more digits
  • .* - the rest of the line
  • \R - a line break char (sequence)
  • (\1-\d.*\R)*+ - zero or more sequences of:
    • \1 - same value as captured into Group 1
    • -\d - a hyphen and a digit
    • .*\R - the rest of the line with a line break char (sequence)
  • \K - a match reset operator discarding all the text matched so far in the current iteration
  • (?!\1-\d)\d+ - one or more digits, but not the same number as captured into group 1 and followed with - and a digit

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563