Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch and replace script on a given portion of text
    primarykey
    data
    text
    <p>I'm manipulating text files and need to build a perl script to run a search replace only on the notes of a given portion of text (chapters in this case), so that I can convert this pattern: </p> <pre><code>Chapter 1:1 text here(Note a) more text here(Note b) 2 text here(Note c) more text here(Note d) 3 text here(Note e) more text here(Note f) 4 text here(Note g) more text here(Note h) Chapter 2:1 text here(Note i) more text here(Note j) 2 text here(Note k) more text here(Note l) 3 text here(Note m) more text here(Note n) 4 text here(Note o) more text here(Note p) 5 text here(Note q) more text here(Note r) 6 text here(Note s) more text here(Note t) </code></pre> <p>into this:</p> <pre><code>Chapter 1:1 text here(Note a) more text here(Note b) 2 text here(Note c) more text here(Note d) 3 text here(Note e) more text here(Note f) 4 text here(Note g) more text here(Note h) Chapter 2:1 text here(Note a) more text here(Note b) 2 text here(Note c) more text here(Note d) 3 text here(Note e) more text here(Note f) 4 text here(Note g) more text here(Note h) 5 text here(Note i) more text here(Note j) 6 text here(Note k) more text here(Note l) </code></pre> <p>In other words, I need to set the 'counter' of each note to "a" at the start of each new chapter. The following regex matches every chapter:</p> <pre><code>(?s)^\w{1,10} \d{1,3}:\d{1,3}.+?\(Note \w\).+?(?=\w{1,10} \d{1,3}:\d{1,3}) </code></pre> <p>and I have tried to use a while loop like this:</p> <pre><code>my @notes = ('Note a', 'Note b', 'Note c', 'Note d'); $Count = a; foreach my $Marker (@notes) { $_=~s/(\\(Note\\))[a-z]/"$1".$Count++/e; } </code></pre> <p>But I am stuck and cannot possibly think of a way to build a script in such a way that it stops at every chapter division and then starts again until the end. Maybe I'm using the wrong approach?</p> <p>What do I need to do in order to apply the search and replace only to each chapter, as shown above (i.e., the first regex)?</p> <p>Any help will be greatly appreciated. Thanks!</p> <p><strong>EDIT (July 30)</strong></p> <p>Both answers work fine. I voted the first one as my favorite because I understand the logic better, but BOTH are equally valid.</p> <p>Now, as a corollary to my first question. How could I easily include the chapter name and chapter number <strong>sequentially</strong> before each line? Like this:</p> <pre><code>Chapter 1:1 text here(Note a) more text here(Note b) Chapter 1:2 text here(Note c) more text here(Note d) Chapter 1:3 text here(Note e) more text here(Note f) Chapter 1:4 text here(Note g) more text here(Note h) Chapter 2:1 text here(Note a) more text here(Note b) Chapter 2:2 text here(Note c) more text here(Note d) Chapter 2:3 text here(Note e) more text here(Note f) Chapter 2:4 text here(Note g) more text here(Note h) Chapter 2:5 text here(Note i) more text here(Note j) Chapter 2:6 text here(Note k) more text here(Note l) </code></pre> <p>Do I need to use a variable and increment it or is there a simpler approach?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload