[ create a new paste ] login | about

Link: http://codepad.org/mSIYCw4R    [ raw code | output | fork ]

Perl, pasted on Aug 24:
$str = "Heading
Chapter 1:1 This is text
2 This is more text
3 This is more text
4 This is more text
5 This is more text
6 This is more text
7 This is more text
8 This is more text
9 This is more text
10 This is more text
11 This is more text
12 This is more text
13 This is more text
14 This is moret text 
15 This is more text
Heading
Chapter 2:1 This is text
2 This is more text...
Heading
Chapter 3:1 This is text
2 This is more text...";

$str =~s/
    ((?:^|\n)Heading)   #Capture Heading into group 1.
                        #We can't use lookbehind because of (?:^|\n)
    (?=                 #A lookahead, but don't capture.
      \nChapter\s       #Find the Chapter text.
      (\d+:\d+)         #Get the first chapter text. and store in group 2
      .*                #Capture the rest of the Chapter line.
      (?:\n(\d+).+)+    #Capture every chapter line.
                        #The last captured chapter number gets stored into group 3.
    )
    /\1 (Chapter \2-\3)/gx;
print $str;


Output:
Heading (Chapter 1:1-15)
Chapter 1:1 This is text
2 This is more text
3 This is more text
4 This is more text
5 This is more text
6 This is more text
7 This is more text
8 This is more text
9 This is more text
10 This is more text
11 This is more text
12 This is more text
13 This is more text
14 This is moret text 
15 This is more text
Heading (Chapter 2:1-2)
Chapter 2:1 This is text
2 This is more text...
Heading (Chapter 3:1-2)
Chapter 3:1 This is text
2 This is more text...


Create a new paste based on this one


Comments: