Community
    • Login

    [New Plugin] MultiReplace

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    67 Posts 11 Posters 13.7k Views
    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.
    • Thomas KnoefelT
      Thomas Knoefel @Alan Kilborn
      last edited by

      @Alan-Kilborn said in [New Plugin] MultiReplace:

      Should also consider a 32-bit build for our N++ family friends that use that one.

      All versions will ultimately be made available as “release” builds when plugin reaches 1.0.0.0 . I have updated RC5 now with all release builds. For all commits, I have an Action preparing the builds in the Git repository, which can be download here as well for the latest RC . (Kudos to @Michael-Vincent for his support with this.)

      1 Reply Last reply Reply Quote 4
      • Thomas KnoefelT
        Thomas Knoefel
        last edited by Thomas Knoefel

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • tomiskT
          tomisk
          last edited by

          It seems that regex replace with capture group ($1) doesn’t work. Would love to have this feature.

          PeterJonesP Thomas KnoefelT 2 Replies Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @tomisk
            last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • Thomas KnoefelT
              Thomas Knoefel @tomisk
              last edited by Thomas Knoefel

              This post is deleted!
              1 Reply Last reply Reply Quote 1
              • Thomas KnoefelT
                Thomas Knoefel
                last edited by Thomas Knoefel

                I’ve recently completed the Scope Function for the MultiReplace plugin. It lets you set the area (or scope) for Find, Replace, and Marking. Here’s what’s new:

                Download Pre-Release here:

                @tomisk: Fix for capturing groups is include in this Pre-Release. Thanks for pointing it out,

                Selection Scope:

                • Supports single, multi, and rectangular selections.

                Column Scope:

                • Use the Replace, Find, and Mark functions within distcinct columns of delimited files. Plus, there’s an added highlight feature for these columns.
                • Just a note: The file remains editable, even when the plugin is importing a CSV structure.

                Marking Performance:

                From my observations, highlighting seems a bit slow in N++, especially when marking a large number of unique terms. Thus, I’ve modified the plugin to allow users to cancel the marking process if required.If you cancel and then make edits to unmarked columns, they’ll automatically get highlighted inplace, regardless of their position in the file.
                However, highlighting columns isn’t necessary for any operations. It primarily aids in visualization.

                DemoCSVMultiReplace.gif

                Thomas KnoefelT 1 Reply Last reply Reply Quote 5
                • Thomas KnoefelT
                  Thomas Knoefel @Thomas Knoefel
                  last edited by Thomas Knoefel

                  CSV Support has reached its final goal and will be released soon.

                  Planned Features

                  m looking at introducing more flexibility in replacements. To achieve this, I am considering introducing a checkbox feature named “Contextual Mode” which, when activated, will employ a set of external parameters for more sophisticated and context-aware replacements. Here’s what I have in mind:

                  External Parameters:

                  When “Contextual Mode” is enabled, the following set of external parameters can be utilized in conjunction with replacement strings to provide more context and flexibility:

                  Parameter Description
                  $CNT Number of the string to be replaced.
                  $LINE Line position of the string to be replaced.
                  $APOS Absolute position.
                  $LPOS Position in line.
                  $COL Column position of the string to be replaced. (works only if CSV Scope is enabled)
                  Examples:

                  Use Case 1: Simple Replacement with Numbering:

                  Find String Replace String Before After
                  key $CNT key key key 1 2 3

                  Use Case 2: Regex Combination with External Parameters:

                  Find String Replace String Before After
                  \d+(key) $CNT$1 1key key 4key 1key key 2key

                  Use Case 3: Conditional Replacements:

                  Find String Replace String Before After
                  key $if(CNT = 1, "newString", "key") key key key newString key key

                  The conditions should be < > =, later I’m aiming to expand the condition features with AND and OR , allowing users to target sections in their code with more precision.

                  Simplicity is my focus because complex syntax can make things more challenging.

                  I’d love to hear your insights on this approach. Do you find it beneficial? Additionally, are there any elements or strategies I should consider adding?

                  CoisesC 1 Reply Last reply Reply Quote 1
                  • guy038G
                    guy038
                    last edited by guy038

                    Hello, @thomas-knoefel and All,

                    I do support your future Contextual Mode feature ! This would greatly enhance the N++ Replace function ;-))


                    Now, I’m thinking about about a replacement feature, which had been requested many times : the possibility to add counters in replacement )

                    For instance the simple regex S/R :

                    FIND [\d.]+

                    REPLACE $0\t(Test $NUM)

                    would change this list :

                    12
                    5.3
                    0
                    7.8
                    

                    into :

                    12	(Test 1)
                    5.3	(Test 2)
                    0	(Test 3)
                    7.8	(Test 4)
                    

                    Of course, this new meta-data $NUM should probably support parameters to take the start number and the increment in account !

                    Refer to the part Counters and Cycles of the following FAQ :

                    https://community.notepad-plus-plus.org/topic/23170/faq-desk-can-i-do-a-mathematical-replacement

                    To find out useful ideas about this problem. But, as you said : simplicity is my focus !


                    Note that the $NUM is slightly different from your external parameter $CNT. Of course, @thomas-knoefel, my request is just a suggestion. Do as you like !

                    Best Regards,

                    guy038

                    As a remainder, the following syntaxes are reserved, in the replacement zones, by the Notepad++'s Boost regular expression engine :

                    • $0, ${0}, $&, $MATCH, ${^MATCH}
                    • $ followed with a backtick , $PREMATCH, ${^PREMATCH}
                    • $', $POSTMATCH, ${^POSTMATCH}
                    • $^N, $LAST_SUBMATCH_RESULT, ${^LAST_SUBMATCH_RESULT}
                    • $+, $LAST_PAREN_MATCH, ${^LAST_PAREN_MATCH}
                    • $$
                    • \l, \u, \L, \U, \E
                    • \n ( with 1 <= n <= 9 ) and $N or ${N} ( with N > 0 )
                    • $+{Name}, where Name is a named group
                    • (?N......) and (?N......:......), where N represents the numbered group N
                    • (?{N}......) and (?{N}......:......), where N represents the numbered group N
                    • (?{Name}......) and (?{Name}......:......), where Name represents the named group N

                    See : https://www.boost.org/doc/libs/1_80_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

                    1 Reply Last reply Reply Quote 1
                    • CoisesC
                      Coises @Thomas Knoefel
                      last edited by

                      @guy038,
                      @Thomas-Knoefel said in [New Plugin] MultiReplace:

                      I’d love to hear your insights on this approach. Do you find it beneficial? Additionally, are there any elements or strategies I should consider adding?

                      It looks like you and I are approaching related targets from different directions. I’m working on a plugin, Columns++, with the primary focus being documents with data arranged visually in columns. I introduced the notion of formulas for creating new columns based on calculations from existing columns. There is also a search-and-replace feature that works with rectangular selections.

                      In my last update, I extended the replace capability when using regular expressions to include formula substitutions of the form (?=formula) or (?=format:formula). Details are in the Formulas section of the documentation.

                      A simple example would be:
                      Find what: (?<=\bID)\d+\b
                      Replace with: (?=this+120)
                      to find all cases of “ID” followed by one or more digits, delimited by word boundaries, and add 120 to the number.

                      I didn’t aim for simplicity, though. I gave up on that early (while I was still implementing column calculations) and decided instead to find a free and open source library of some sort that would avoid having to write my own expression parser. I chose ExprTk; so far, it is working well.

                      Thomas KnoefelT 1 Reply Last reply Reply Quote 0
                      • Thomas KnoefelT
                        Thomas Knoefel @Coises
                        last edited by

                        @Coises said in [New Plugin] MultiReplace:

                        I didn’t aim for simplicity, though. I gave up on that early (while I was still implementing column calculations) and decided instead to find a free and open source library of some sort that would avoid having to write my own expression parser. I chose ExprTk ; so far, it is working well.

                        I’m aiming for something more basic as it is mainly a Replacement Tool which is not deeply rooted in complex calculations but can handle basic counting operations. I’m trying to avoid using external Libraries right now (as long as possible)

                        This is my proposed structure:

                        1. Calculation Block
                        $calc(VARIABLE_NAME = EXPRESSION)
                        

                        Manages mathematical operations with a focus on simplicity.
                        $calc(NUM = $CNT + 5)
                        2. Conditional Block

                        $if(CONDITION, THEN_OUTPUT, ELSE_OUTPUT)
                        

                        Allows for the inclusion of calculated variables within conditions or outputs.
                        $if($NUM > 10, “Value $NUM is greater”, “Value $NUM is smaller”)
                        3. String Block

                        $str(STRING_EXPRESSION)
                        

                        Efficiently merges calculated values with strings for the final replacement string. Can be used instead of an if Conditional Block. So either if or str to pass a replacement string.
                        $str(“Total: $NUM key”)

                        One potential challenge might be the extraction of regex groups by numbers, like $1, $2, etc., before sending them to the replacement string. Scintilla doesn’t support this with its Regex searches. But actually, this isn’t my primary concern.

                        @guy038, thank you for highlighting these facts. They’ve provided valuable insights. The NUM example, in particular, inspired the concept of new Command Blocks.

                        I hope that Command Blocks give the syntax more transparency in reading and even more robustness for parsing.

                        CoisesC 1 Reply Last reply Reply Quote 0
                        • guy038G
                          guy038
                          last edited by guy038

                          Hello, @coises, @thomas-knoefel,

                          Oh…, I’m terribly sorry, @coises : I forgot your valuable Columns++ plugin which already provides such a solution to my request. A special mention, too, for your excellent documentation support ;-))


                          To be honest with you both, I’ve been following, for a while, the Notepad++'s development and its plug-ins, in a rather theorical way, on two machines :

                          • An old Win XP machine, with Notepad++ v7.9.2 32-bit x86

                          • A recent Win 10 laptop, with Notepad++ v8.5.4 64-bit x64, located on an USB drive

                          On these two versions, I just add the BetterSelection, ComparePlus and Elastic Tabstops plugins and the main script plugins : LuaScript, NppExec and Python Script

                          I’m still in the process of cleaning up my old computer, before transferring “clean” data on my new laptop. ( Don’t worry, @alan-kilborn, I’m nearing the end : the 785 and 792 still need to be reviewed in depth, as well as the deletion of multiple copy files… before the definitive migration ! )

                          In addition, I’m presently involved in managing all my holiday photos, since June. In short, for all these reasons, I have not installed the last N++ version yet, as well as your two plugins Columns+++ and MultiReplace :-((


                          So, the best thing I can do is to move on, once and for all, to the 21st century, which will allow me to make more accurate suggestions regarding your two excellent plugins !!

                          Best Regards,

                          guy038

                          1 Reply Last reply Reply Quote 1
                          • CoisesC
                            Coises @Thomas Knoefel
                            last edited by

                            @Thomas-Knoefel said in [New Plugin] MultiReplace:

                            One potential challenge might be the extraction of regex groups by numbers, like $1, $2, etc., before sending them to the replacement string. Scintilla doesn’t support this with its Regex searches. But actually, this isn’t my primary concern.

                            Unless I misunderstand what you mean, SCI_GETTAG does this.

                            Thomas KnoefelT 1 Reply Last reply Reply Quote 2
                            • Thomas KnoefelT
                              Thomas Knoefel @Coises
                              last edited by Thomas Knoefel

                              @Coises Wow, you’re always full of good ideas! :-) Thanks! I hope I can share some of mine with you too. :-))

                              Thomas KnoefelT 1 Reply Last reply Reply Quote 1
                              • Thomas KnoefelT
                                Thomas Knoefel @Thomas Knoefel
                                last edited by

                                The first functional release is now available for testing and can be downloaded here.

                                While I’ve made some changes from the original concept, it still closely aligns with that initial vision. This Dynamic Substitution is compatible with all search settings of Search Mode, Scope, and the other options.

                                By activating the 'Use Variables’ checkbox, variables associated with specified strings can be employed, allowing for conditional and computational operations within the replacement string.
                                Use_Variables.gif

                                Variables Overview

                                Variable Description
                                CNT Count of the detected string.
                                LINE Line number where the string is found.
                                APOS Absolute character position in the document.
                                LPOS Relative line position.
                                COL Column number where the string was found (CSV-Scope option selected).
                                CAP1, CAP2, … Correspond to capture groups $1, $2, … in regex patterns. They can be used for calculations or as strings.
                                Note: Both dot and comma can be used as decimal separators in Capture Variables but thousands separators are not supported.

                                Command Overview

                                String Composition

                                .. is employed for concatenation.
                                E.g., "Detected "..CNT.." times."

                                set(strOrCalc)

                                Outputs strings or numbers directly.

                                Example Result (assuming LINE = 5, CNT = 3)
                                set("replaceString"..CNT) “replaceString3”
                                set(LINE+5) “10”
                                cond(cond, trueVal, [falseVal])

                                Implements if-then-else logic, or if-then if falseVal is omitted.

                                Example Result (assuming LINE = 5)
                                cond(LINE<=5 or LINE>=9, "edge", "center") “edge”
                                cond(LINE<3, "Modify this line") (Original text remains unchanged)
                                cond(LINE<10, cond(LINE<5, cond(LINE>2, "3-4", "0-2"), "5-9"), "10+") “5-9” (Nested condition)
                                fmtN(num, maxDecimals, fixedDecimals)

                                Formats numbers based on precision (maxDecimals) and whether the number of decimals is fixed (fixedDecimals being true or false).

                                Note: The fmtN command can exclusively be used within the set and cond commands.

                                Example Result
                                set(fmtN(5.73652, 2, true)) “5.74”
                                set(fmtN(5.0, 2, true)) “5.00”
                                set(fmtN(5.73652, 4, false)) “5.7365”
                                set(fmtN(5.0, 4, false)) “5”

                                Operators

                                Type Operators
                                Arithmetic +, -, *, /, ^, %
                                Relational ==, ~=, <, >, <=, >=
                                Logical and, or, not

                                Engine Overview

                                MultiReplace uses the Lua engine, allowing for Lua math operations and string methods. Refer to Lua String Manipulation and Lua Mathematical Functions you can use for more information.

                                Thomas KnoefelT 1 Reply Last reply Reply Quote 2
                                • Thomas KnoefelT
                                  Thomas Knoefel @Thomas Knoefel
                                  last edited by

                                  I have incorporated two additional variables which I find beneficial:

                                  • LCNT: Count of the detected string within the line.
                                  • MATCH: Contains the text of the detected string, in contrast to CAP variables.

                                  The complete documentation can be found here.

                                  Should anyone have ideas for additional variables that are missing and should be included into this option, feel free to share.

                                  Below are some examples demonstrating how to utilize the “Use Variables” option.

                                  Find in: Replace with: Description/Expected Output Regex Scope CSV
                                  ; cond(LCNT==5,";Column5;") Adds a 5th Column for each line into a ; delimited file. No No
                                  (\d+) set("$1€ The VAT is: ".. (CAP1 * 0.15).."€ Total with VAT: ".. (CAP1 + (CAP1 * 0.15)).."€") Finds a number and calculates the VAT at 15%, then displays the original amount, the VAT, and the total amount. E.g., 50 becomes 50€ The VAT is: 7.5€ Total with VAT: 57.5€ Yes No
                                  --- cond(COL==1 and LINE<3, "0-2", cond(COL==2 and LINE>2 and LINE<5, "3-4", cond(COL==3 and LINE>=5 and LINE<10, "5-9", cond(COL==4 and LINE>=10, "10+")))) Replaces --- with a specific range based on the COL and LINE values. E.g., 3-4 in column 2 of lines 3-4, and 5-9 in column 3 of lines 5-9 assuming --- is found in all lines and columns. No Yes
                                  (\d+)\.(\d+)\.(\d+) cond(CAP1 > 0 and CAP2 == 0 and CAP3 == 0, MATCH, cond(CAP2 > 0 and CAP3 == 0, " " .. MATCH, " " .. MATCH)) E.g., 1.0.0 is aligned left, 1.2.0 is prefixed with a space, shifting right, indicating a second-level version change. Yes No
                                  (\d+) set(CAP1 * 2) Doubles the matched number. E.g., 100 becomes 200. Yes No
                                  ; cond(LCNT == 1, string.rep(" ", 20- (LPOS))..",") Inserts spaces before the semicolon to align it to the 20th character position if it’s the first occurrence. No No
                                  - cond(LINE == math.floor(10.5 + 6.25 * math.sin((2 * math.pi * LPOS) / 50)), "*", " ") “For the fun part ;-)” Draws a sine wave across a canvas of ‘-’ characters spanning at least 20 lines and 80 characters per line. No No
                                  1 Reply Last reply Reply Quote 2
                                  • Thomas KnoefelT
                                    Thomas Knoefel
                                    last edited by

                                    After intensive testing, Release 2.0.07 is complete and is shipped with the new Notepad++ release 8.5.8.

                                    New Functionalities:

                                    • CSV Syntax Highlighting: Provides visual formatting to delimited files, aiding in the readability and structure recognition of the data.

                                    • CSV Column Scope: Allows specific column targeting for search and replace in any delimited files.

                                    • Rectangular and Multi-selections Support: Extends search and replace to specified rectangular or multi-selected areas.

                                    • Conditional and Computational Functionality via “Use Variables” Option: Enables dynamic replacement strings using variables for conditional and computational operations, facilitated by simple set and cond commands.

                                    Vitalii DovganV 1 Reply Last reply Reply Quote 2
                                    • Vitalii DovganV
                                      Vitalii Dovgan @Thomas Knoefel
                                      last edited by

                                      @Thomas-Knoefel
                                      I’ve noticed that the latest archives (e.g. MultiReplace-v2.0.0.7-x64.zip and MultiReplace-v2.0.0.7-Win32.zip) contain pretty old README.md that does not mention all the amazing new features.

                                      And it could be a good idea to add some kind of interactive help. Maybe in a form of popup hints or maybe as a clickable [?] button that allows to “apply” the ‘?’ sign to an UI element by showing the corresponding help text when applied.

                                      Thomas KnoefelT 1 Reply Last reply Reply Quote 3
                                      • Thomas KnoefelT
                                        Thomas Knoefel @Vitalii Dovgan
                                        last edited by

                                        @Vitalii-Dovgan said in [New Plugin] MultiReplace:

                                        I’ve noticed that the latest archives (e.g. MultiReplace-v2.0.0.7-x64.zip and MultiReplace-v2.0.0.7-Win32.zip) contain pretty old README.md that does not mention all the amazing new features.

                                        Ah, now that you mention it, I realize I updated the README after the release, which was an oversight on my part. Thanks for catching that. There’s a new release coming with the next N++ version, which will introduce “Replace in all open documents”. I’ll make sure to get the order right.then.

                                        @Vitalii-Dovgan said in [New Plugin] MultiReplace:

                                        And it could be a good idea to add some kind of interactive help. Maybe in a form of popup hints or maybe as a clickable [?] button that allows to “apply” the ‘?’ sign to an UI element by showing the corresponding help text when applied.

                                        Thanks for the suggestion. My initial idea on that is to add a Question Mark Button next to ‘Use Variables’. This would launch a small help window with different sections like Commands, Options, Operators, Math Functions, and String Functions. Each section would be foldable and offer a description of each list entry. Clicking on a specific entry would insert its function at the cursor position in the Replace String Section.

                                        Thomas KnoefelT 1 Reply Last reply Reply Quote 3
                                        • Thomas KnoefelT
                                          Thomas Knoefel @Thomas Knoefel
                                          last edited by Thomas Knoefel

                                          The next MultiReplace release 2.1.0.8 will be shipped with the next Notepad++ version 8.5.9

                                          These are the release notes:

                                          • Added feature: “Replace All across all open documents”.
                                          • Enhancement: Added tooltips to column headers in lists.
                                          • Optimization: Removed “Normal” column due to redundant information.
                                          • Bug fix: Resolved issue with “Extended” in combination with “Use Variables.”
                                          Thomas KnoefelT 1 Reply Last reply Reply Quote 2
                                          • Alan KilbornA Alan Kilborn referenced this topic on
                                          • Thomas KnoefelT
                                            Thomas Knoefel @Thomas Knoefel
                                            last edited by Thomas Knoefel

                                            I wish you all a Happy New Year.!

                                            The next MultiReplace 2.2.0.9 version will be shipped with Notepad++ 8.6.1 or can be dowloaded here.

                                            Release Notes for 2.2.0.9:

                                            • “Replace first match only” checkbox: I’ve noticed that this option will be occasionally used, especially when replacing strings with the same find pattern in a list, but with different replace strings. Although achievable by the ‘Use Variables’ option using cond(CNT==1,‘Replace String’).

                                            • Help Button for 'Use Variables’: A new help button, is now included on the panel.
                                              A big thanks from here to @Coises Coises, the creator of the Columns++ Plugin, providing the template for the HTML help page.

                                            • Enhancement in ‘Use Variables’ with init({}): This feature is closing the gap for self-defined variables. These Variables are integrated into the entire Replace-All process and across all list run replacements.

                                            The init command introduces a way to initialize newly defined variables, ensuring the value is set just once. With this, the newly introduced if-Then feature becomes significantly more useful, especially when combined with other available variables.

                                            1st example:
                                            It is summing digits of Column2 and Column4 and Replaces the 4th found digit in the line:
                                            Find: (\d+)
                                            Replace: init({COL2=0,COL4=0}); cond(LCNT==4, COL2+COL4); if COL==2 then COL2=CAP1 end; if COL==4 then COL4=CAP1 end;

                                            Output:
                                            1,20,text,2,0 -> 1,20,text,2,22.0

                                            2nd example:
                                            Removes duplicate lines, keeping the first occurrence of each line. Like discussed in this Post.
                                            Find: ^(.*)$
                                            Replace: init({MATCH_PREV=1}); cond(MATCH == MATCH_PREV, ''); MATCH_PREV=MATCH;

                                            Thomas KnoefelT 1 Reply Last reply Reply Quote 1
                                            • Alan KilbornA Alan Kilborn referenced this topic on
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors