Search code examples
regexfindmultiline

Matching line where the line number is the sum of constants and the value of a certain line content (in a variable multiline text)


I have this file below, which is 3 recipes. Each recipe contains a fixed number of header lines and a variable number of ingredient lines (composition). The last line of the header gives the number of the ingredients in the composition. This line is always the 16th line of the header (in the linked screenshot these are lines 16, 38, 58). Then comes the composition with the number of lines as defined in line 16 of the header. Then the last line of each recipe is always 0,

I need to separate the recipes, so I need to find the last lines, which are line 22, 42 68 in the example. The number of lines for each recipe is: 16 lines of header + the number of the ingredients (integer defined in line 16 of the header + 1 line (the final 0,)

screenshot of the text

I only got this far:

(?:.*?^0\,){1}

https://regex101.com/r/js3leQ/1

But this only finds all the lines with 0,. Actually it would be good to find once the third 0, line match. That would already give me the solution I need (since the third 0, is always the last line of a recipe).


Solution

  • To identify the ending row (3rd zero), the following may work, if you were able to only have regex g flag (without m)

    0,(?=\n"REC|$)
    

    https://regex101.com/r/ufXYFU/1

    Update. According to the comment that "1", always precedes the 0,, the following should probably work (it would work even if m flag was present):

    (?<="1",\n)0,
    

    https://regex101.com/r/aJk5zM/1