Community
    • Login

    Replace characters only when a certain number exist

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    11 Posts 4 Posters 1.0k 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.
    • Alan KilbornA
      Alan Kilborn @Jim Miller
      last edited by

      @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 Reply Last reply Reply Quote 2
      • Terry RT
        Terry R
        last edited by 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 Reply Last reply Reply Quote 2
        • guy038G
          guy038
          last edited by 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 Reply Last reply Reply Quote 3
          • Jim MillerJ
            Jim Miller @Terry R
            last edited by

            @Terry-R First and third instance of pipe

            1 Reply Last reply Reply Quote 0
            • Jim MillerJ
              Jim Miller @guy038
              last edited by

              @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 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn
                last edited by

                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 Reply Last reply Reply Quote 2
                • guy038G
                  guy038
                  last edited by 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 Replies Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @guy038
                    last edited by

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

                    I don’t really understand the RegexBuddy comment

                    Ha. Me either!

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

                      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 Reply Last reply Reply Quote 0
                      • Jim MillerJ
                        Jim Miller @guy038
                        last edited by

                        @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 Reply Last reply Reply Quote 2
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors