Community
    • 登入

    Replace characters only when a certain number exist

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    11 貼文 4 Posters 1.3k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Jim MillerJ
      Jim Miller
      最後由 編輯

      Hello

      I was wondering if it is possible to do an advance find and replace where NP++ looks at each line, counts the number of times a character exists and if certain criteria are met, do a replace.

      I want to look for lines that have 5 pipes | and if 5 exist, replace number 1 and 3 with a comma. Is this possible?

      Alan KilbornA 1 條回覆 最後回覆 回覆 引用 0
      • Alan KilbornA
        Alan Kilborn @Jim Miller
        最後由 編輯

        @Jim-Miller

        This will find the lines with exactly 5 | characters:

        find: ^(?:([^|\r\n]*)\|){5}(?1)\R
        search mode: regular expression

        But I’m not sure how to replace arbitrary ones of them! :-)

        More info in THIS thread about this kind of thing.

        1 條回覆 最後回覆 回覆 引用 2
        • Terry RT
          Terry R
          最後由 Terry R 編輯

          @Jim-Miller said in Replace characters only when a certain number exist:

          I want to look for lines that have 5 pipes | and if 5 exist, replace number 1 and 3 with a comma.

          I believe I have created a regex (regular exression) which will replace the numbers 1 and 3 if there are 5 | pipes in a line. However some caveats.

          1. If more than 5 pipes it will still replace any 1 and/or 3 on that line.
          2. It needs to run multiple times as ONLY replaces 1 number on each line for each run. If your lines will ONLY have 1 of the numbers then possibly only 1 run required.
          3. As it uses the \K combination it MUST be run with the “Replace All” button.

          Given that, here is what I have:
          Find What:(?-s)^(?=.*(?:\|.*){5}).*\K(1|3)
          Replace With:,

          As stated before it’s a regex so search mode must be “regular expression”. Keep clicking on the “Replace All” button until the bottom of the window states “Replace All: 0 occurances were replaced in entire file”.

          See if that helps at all.

          Terry

          PS edit, I assumed the actual number 1 and 3 were to be replaced, however I think I see @Alan-Kilborn assumes that the 1st and 3rd instance of the pipe character are to be replaced. Which is it?

          Jim MillerJ 1 條回覆 最後回覆 回覆 引用 2
          • guy038G
            guy038
            最後由 guy038 編輯

            Hello, @jim-miller, @Alan-kilborn, @terry-r and All,

            I assume, like @alan-kilborn, that you want change the first and third | char, of any line, with a comma ,, ONLY IF current line contains, at least, five | characters

            I propose this regex version :

            SEARCH ^([^|\r\n]*)\K\|((?1)\|(?1))\|(?=(?2)\|)

            REPLACE ,\2,

            You may use as well, for the search regex, the free-spacing mode, below, with some explanations , in comments. Just select all that block and open the Replace dialog ( Ctrl + H )

            (?x)               # FREE-SPACING mode
            ^                  # BEGINNING of line
            (                  # START Group 1
            [^|\r\n]*          #   ANY range, possibly NULL, of chars, DIFFERENT from PIPE | and EOL chars, till ...
            )                  # END   Group 1
            \K                 # RESET of current MATCH, so far
            \|                 # ... a LITERAL PIPE char
            (                  # START Group 2
            (?1)  \|  (?1)     #   "Group 1 REGEX", followed by a LITERAL PIPE char and followed by an other "Group 1 REGEX", till ...
            )                  # END   Group 2
            \|                 # ... a LITERAL PIPE char
            (?= (?2)  \| )     # ONLY IF "Group 2 REGEX", followed by a LITERAL PIPE char ( The FIFTH PIPE char ) is found, RIGHT AFTER
            

            Important :

            • Whatever the search syntax used, You must click, exclusively, on the Replace All button ( Do not use the Replace button )

            • Select the Regular expression search mode, of course

            • Tick, preferably, the Wrap around option

            Best Regards,

            guy038

            Jim MillerJ 1 條回覆 最後回覆 回覆 引用 3
            • Jim MillerJ
              Jim Miller @Terry R
              最後由 編輯

              @Terry-R First and third instance of pipe

              1 條回覆 最後回覆 回覆 引用 0
              • Jim MillerJ
                Jim Miller @guy038
                最後由 編輯

                @guy038 said in Replace characters only when a certain number exist:

                Am unsure if this will have worked but it causes my NP++ to crash. I updated to the latest version as I knew that I was a few versions behind but this gave the same result.

                1 條回覆 最後回覆 回覆 引用 0
                • Alan KilbornA
                  Alan Kilborn
                  最後由 編輯

                  I don’t see a Notepad++ crash with @guy038 's regex:

                  (?x)^([^|\r\n]*)\K\|((?1)\|(?1))\|(?=(?2)\|)

                  In fact, I don’t know of any regex you could specify that has been known to cause a N++ crash before??

                  But anyway, RegexBuddy complains about the regex, saying for the second (?1) that: Recursive calls need to be optional or the called group needs an alternative without recursion for the group to be able to match anything

                  …so, I think there is something funky going on with this.

                  1 條回覆 最後回覆 回覆 引用 2
                  • guy038G
                    guy038
                    最後由 guy038 編輯

                    Hi, @jim-miller, @Alan-kilborn, @terry-r and All,

                    To @alan-kilborn :

                    I don’t really understand the RegexBuddy comment ?!

                    Below, I indicated :

                    • In the second line, where the two groups 1 and 2 are defined

                    • In the third line, the regex

                    • In the fourth line, where the subroutine calls are used

                    Definitions:      Gr_1           Gr_2
                                    _________      __________
                    Regex:        ^([^|\r\n]*)\K\|((?1)\|(?1))\|(?=(?2)\|)
                                                   ¯¯¯¯  ¯¯¯¯      ¯¯¯¯
                    Utilizations:                  Gr_1  Gr_1      Gr_2
                    

                    And, we cannot see any subroutine call (?#), located within its own group # (...). So this clearly shows that this regex does not use any recursive syntax !

                    In order to get a recursive syntax, you need, for instance, this kind of regex construction :    .......(......(?1)..)...   , with no parenthesis in any “dot” zones, with the (?1) located inside the parentheses zone defining the Group1 !


                    To all :

                    Moreover, I tried with a small set of lines with pipe chars that I duplicated many times in order to get a 12,397,000 bytes file, containing about 506,000 lines.

                    Applying the regex S/R, with the free-spacing mode, against that file was successful, after 303,600 replacements in 57 s, due to my old Win XP SP3 machine ;-) And, of course, N++ did not crash at all !

                    Best Regards

                    guy038

                    P.S. :

                    In the short regex form, without the free-spacing mode, I simply omitted the useless modifier (?x), as there is no space char !

                    Alan KilbornA Jim MillerJ 2 條回覆 最後回覆 回覆 引用 1
                    • Alan KilbornA
                      Alan Kilborn @guy038
                      最後由 編輯

                      @guy038 said in Replace characters only when a certain number exist:

                      I don’t really understand the RegexBuddy comment

                      Ha. Me either!

                      1 條回覆 最後回覆 回覆 引用 0
                      • guy038G
                        guy038
                        最後由 編輯

                        Hi, @alan-kilborn and All,

                        To be more accurate :

                        As a matter of fact, the RegexBuddy’s statement “Recursive calls need to be optional OR the called group needs an alternative without recursion for the group to be able to match anything” is totally exact

                        But I still do not see how this could apply to my regular expression, given the observations mentioned in my previous post. !

                        BR

                        guy038

                        1 條回覆 最後回覆 回覆 引用 0
                        • Jim MillerJ
                          Jim Miller @guy038
                          最後由 編輯

                          @guy038
                          I tried to run it on another machine and it executed successfully. I suppose I should have tried the first ;-)
                          Anyway, thank you very much for your help

                          1 條回覆 最後回覆 回覆 引用 2
                          • 第一個貼文
                            最後的貼文
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors