• Login
Community
  • Login

Select every 3rd character. Then delete them. How can I do this?

Scheduled Pinned Locked Moved General Discussion
search & replace
9 Posts 5 Posters 2.3k 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.
  • H
    Hye Kan Chu
    last edited by Apr 7, 2019, 1:29 AM

    I have only one really long line and I want to
    Select every 3rd character. Then delete them. The characters could be anything.
    How would i do that?

    M 1 Reply Last reply Apr 7, 2019, 8:53 AM Reply Quote 0
    • M
      Meta Chuh moderator @Hye Kan Chu
      last edited by Meta Chuh Apr 7, 2019, 9:12 AM Apr 7, 2019, 8:53 AM

      welcome to the notepad++ community, @Hye-Kan-Chu

      here’s a little regex to remove every 3rd character of a long line:

      • make a test copy of your file to try this out.
      • go to the notepad++ menu search > replace and enter the following:

      find what: (.?.?)(.?)
      replace with: $1
      wrap around: enabled
      search mode: regular expression

      • then hit replace all

      a line like 123123123123123 will now look like this: 1212121212.
      or a line like abc123ABC123+#*123 123 like this: ab12AB12+#12 12.

      E 1 Reply Last reply Apr 7, 2019, 12:13 PM Reply Quote 1
      • E
        Ekopalypse @Meta Chuh
        last edited by Apr 7, 2019, 12:13 PM

        @Meta-Chuh

        may I ask you why you used the ? in find what?
        It can’t be a non-greedy operator as there is no multiply operator like * or +, can it be?
        As a dot denotes a single match this (..). should be sufficient, shouldn’t it?
        I know, with regex there are multiple ways to achieve what you want but I
        didn’t expect the ? usage so I’m really curious if this was on purpose and what the reason is.

        M 1 Reply Last reply Apr 7, 2019, 2:36 PM Reply Quote 2
        • M
          Meta Chuh moderator @Ekopalypse
          last edited by Meta Chuh Apr 7, 2019, 2:38 PM Apr 7, 2019, 2:36 PM

          @Ekopalypse

          yes, you are absolutely correct.

          no special reason, just accustomed to using the same full syntax modules for everything for my own readability, even if unnecessary if the op doesn’t request more.
          hence also the second (), to be able to use $2 without changing the preg_match, if needed in the future.

          maybe this is typical for me and other pcre php people to easily read and modify pregs on web servers, so you might see very long preg replaces, where you know they can be simplified a lot, but they are perhaps more modular for adaptations than a simplified version.

          A 1 Reply Last reply Apr 7, 2019, 4:21 PM Reply Quote 2
          • A
            Alan Kilborn @Meta Chuh
            last edited by Apr 7, 2019, 4:21 PM

            @Meta-Chuh

            I fully understand the reason for using extra ( and ). But, and I think I’m agreeing with Eko here, the ? doesn’t make much sense for the problem statement. :)

            M 1 Reply Last reply Apr 7, 2019, 6:31 PM Reply Quote 0
            • M
              Meta Chuh moderator @Alan Kilborn
              last edited by Apr 7, 2019, 6:31 PM

              @Alan-Kilborn

              the following things are supposedly relevant:

              does it work ?
              does it do the job ?
              does the syntax produce any issues ?

              as long as the answers are yes, yes, no, it should be fine.
              if you disagree: i’ve already told eko that he is absolutely correct, so take this answer for you too if you like.

              E 1 Reply Last reply Apr 7, 2019, 7:58 PM Reply Quote 0
              • E
                Ekopalypse @Meta Chuh
                last edited by Apr 7, 2019, 7:58 PM

                @Meta-Chuh

                thank you for clarification, very much appreciated.
                I always have that kind of feeling that I’ve missed something when I see someone
                doing something which I haven’t expected especially in areas where I think I didn’t
                really understand most of it. ;-)

                1 Reply Last reply Reply Quote 1
                • G
                  guy038
                  last edited by guy038 Apr 7, 2019, 8:01 PM Apr 7, 2019, 7:59 PM

                  Hi, @hye-kan-chu, @meta-chuh, @ekopalypse, @alan-kilborn and All,

                  @meta-chuh :

                  Just a small mistake ! Using your regex S/R :

                  SEARCH (.?.?)(.?)

                  REPLACE $1

                  Against your second example abc123ABC123+#*123 123 does give :

                  ab12AB12+#12 13 ( and not ab12AB12+#12 12 ! )

                  Using the following S/R also gives the same results :

                  SEARCH (..).

                  REPLACE \1

                  Cheers,

                  guy038

                  M 1 Reply Last reply Apr 7, 2019, 8:51 PM Reply Quote 3
                  • M
                    Meta Chuh moderator @guy038
                    last edited by Meta Chuh Apr 7, 2019, 9:17 PM Apr 7, 2019, 8:51 PM

                    @guy038

                    you are absolutely correct.

                    this happened due to posting inline code in between ` characters.
                    please press edit on my post and you will see three spaces instead of one when copying the line.

                    good to know that we must avoid inline coding when posting more than one whitespace, thanks for making me have a look at that 👍
                    (maybe this happened in the past as well to others, and was the reason why users could not achieve the same, verified results as we do, making us wonder why)

                    apparently this is a limitation of nodebb not rendering nbsp’s on the canvas when using inline span of code highlighting.

                    original sample code:

                    before:
                    abc123ABC123+#*123   123
                    
                    after:
                    ab12AB12+#12  12
                    

                    ps: my apologies, from now on i will re-copy-paste from a post back to notepad++ to verify a successful, reproducible experience for the op.

                    1 Reply Last reply Reply Quote 2
                    9 out of 9
                    • First post
                      9/9
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors