Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Finding numbers and batch perform arithmetics in text?

    General Discussion
    2
    3
    130
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Ciprian Popa
      Ciprian Popa last edited by Ciprian Popa

      Hi,
      This is the first time address to forum, but after checking I couldn’t find a similar topic, so I start this one with the formulation of the problem:
      I am trying to edit a file (in fact many) that has numbers like in the example format below:
      “1. Introduction/211,Black,notBold,notItalic,open,FitPage
      2. Amino Acid Nitrogen in Sediments/211,Black,notBold,notItalic,open,FitPage
      3. Bulk Characterization of Sediments: C/N/213,Black,notBold,notItalic,open,FitPage
      4. Isotopes and Diagenetic Fractionation/214,Black,notBold,notItalic,open,FitPage
      5. Molecular-Level Isotope Analyses/216,Black,notBold,notItalic,open,FitPage
      References/219,Black,notBold,notItalic,open,FitPage”
      Each “/###” represents a number (page number in a pdf in this example) that needs to be offset by a certain amount. Is there any implemented function that can do that in batch?
      I.e. say /001 + 12 = /013, /009 +12 = /021 etc.

      Alan Kilborn 1 Reply Last reply Reply Quote 0
      • Alan Kilborn
        Alan Kilborn @Ciprian Popa last edited by

        @ciprian-popa said in Finding numbers and batch perform arithmetics in text?:

        Is there any implemented function that can do that in batch?

        Not natively with just Notepad++ itself.
        However, you could do it with a scripting plugin such as PythonScript.
        From the docs is a hint at doing it:

        b279506d-1bfe-4964-9876-81a3c79cf976-image.png

        The rereplace function is called with the regular expression to search for and the function to be called to replace matches.

        So maybe for your case it would look like this:

        def add_12(m):
            new_page = int(m.group(1)) + 12
            return '/{:03}'.format(new_page)
        
        editor.rereplace('/([0-9]{3})', add_12)
        
        Ciprian Popa 1 Reply Last reply Reply Quote 4
        • Ciprian Popa
          Ciprian Popa @Alan Kilborn last edited by Ciprian Popa

          @alan-kilborn Hi Alan,
          Thank you for that piece of code!

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Copyright © 2014 NodeBB Forums | Contributors