Replace value according to previous line

  • Hi guys

    Im editing sfz (a music samples standard), and would need to edit that kind of thing :

    *<region> sample=Samples\Blister Pack5\Baghhopen.wav

    <region> sample=Samples\Blister Pack5\Bagsnes1.wav

    <region> sample=Samples\Blister Pack5\Bagsnes2.wav

    etc etc (hundreds of values)
    What i need to do is knda simple i guess, i need the xx value for “pitch_keycenter=” to be set according to what previous line say (hikey=yy)
    In this case i would need to respectively set the “pitch_center” values, to 0,1 and 2.
    Is there a way to achieve this in np++ ?

    Thanks a million


  • Hello Emmanuel,

    Well, it’s easy enough to achieve that S/R !

    • Go back to the very beginning of your file

    • Open the Replace dialog and choose the Regular Expression search mode

    • In the SEARCH zone, type ((\d+)\Rpitch_keycenter=)\d+

    • In the REPLACE zone type \1\2

    • Click on the Replace All button

    Notes :

    • \d+ represents any number (one or more consecutive digits )

    • \R is any End of Line ( \r\n for a Windows file, \n for an Unix file or \r for an old Mac file

    • The regex (\d+)\Rpitch_keycenter= stands for the group 1, which is re-written, first, (\1), in replacement

    • The first regex \d+ is the group 2 ( the number at the end of the hikey line, before the EOL and pitch_keycenter ), which is re-written, last, (\2), in replacement

    You can either use that other regex syntax :

    SEARCH = (\d+)\Rpitch_keycenter=\K\d+

    REPLACE = \1

    Notes :

    • The regex engine tries to match the regex (\d+)\Rpitch_keycenter=, and \d+ is the group 1 ( the number at the end of the hikey line

    • Then, due to the \K form, the regex engine forget the previous match and tries, now, to match the remaining. That is to say, the simple regex \d+ ( the number, at end of the pitch_keycenter line )

    • This number is, finally, replaced by the value of group 1

    • DON’T use the step-by-step Replace button, with that second syntax

    Hope these regexes will be useful to you !

    Best Regards


  • Wow, thank you very very much, guy038, you not only almost saved my life :), but you took the time to explain things extensively to me, thanks again !


Log in to reply