Run a macro multiple times?

2013-06-18
2013-06-18
  • Frank Chang

    Frank Chang - 2013-06-18

    Hi folks,

    I was introduced to Notepad++ by a colleague of mine and found it a spectacular tool to have. These days I have a few tasks that would work better with macros. Really simple tasks. An example can be given below.

    start of file

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    V7 1.0e+04 2 0

    V8 1.0e+06 2 0

    V9 1.0e+08 2 0

    end of file

    All I really need to do is to change the string "2 0" at the end of each line that starts with "#V7 1.0e+04" into "3 0". I built a macro using the macro recording tool, which can do this once a time. I can run this macro multiple times till the end of the file. There would be no problems. However, I have a file that has thousands of these. I'd like to make it "easier". The option that notepad++ has to run a macro multiple times is very appealing to me.

    However, when I do "run a macro multiple times (till the end of the file)", it does not work. This is what I got.
    "#V7 1.0e+04 3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0
    3 0"

    I am not sure where I missed. What puzzles me is that when running the macro once at a time, it does what it is supposed to do. But not in an automatic fashion. I'd like to have some advices. Thanks!
    My version of notepad++ is 6.3.2. My OS is windows 7 Pro.

    Best Regards,
    Frank

     
  • THEVENOT Guy

    THEVENOT Guy - 2013-06-18

    Hello Frank Chang,

    Notepad++ macros record key strokes very well but there are some issues when using search/replacements :(

    In your case, the use of the new PCRE engine of N++, since the 6.0 version, is quite sufficient to perform your search/replacement !

    So, open the Find/Replace dialog ( CTRL + H )

    Type in :

    SEARCH : ^(?=V7)(.+)2 0
    REPLACE : \13 0

    Tick the Wrap around box

    Choose the Regular expression search mode

    Click on the Replace button for step by step replacement or on the Replace All button for global replacement.

    That's all !!

    NOTES :

    the lookahead (?=V7) search the string 'V7', just after the very beginning of line ^, but doesn't be part of the regex.

    the form .+ represents all the characters, between beginning of line and the string
    '2 0'.

    As it's surrounded by two parenthesis, that syntax stands for group 1.

    In replacement part, the form \1 represent the search group 1.


    A second syntax is possible :

    SEARCH : ^(?=V7).+\K2 0
    REPLACE : 3 0

    The special \K construction "forget" every match of the regex, found BEFORE \K.

    So, the search string is only the string '2 0'. Thus, the replacement string must be, this time, the string '3 0 only.

    IMPORTANT

    Don't use the step by step replacement with this second syntax. Just click on the Replace All button.


    The advanced features of the Perl Common Regular Expressions ( PCRE ), added to N++, are very powerful, but you'll need some months to be well acquainted with !

    Cheers,

    guy038

    P.S. :

    You will find good documentation, about the new Perl Common Regular Expressions (PCRE), used by N++, since the 6.0 version, at the TWO addresses below :

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

    The FIRST link concerns the syntax of regular expressions in SEARCH

    The SECOND link concerns the syntax of regular expressions in REPLACEMENT

     
    Last edit: THEVENOT Guy 2013-06-19