• Login
Community
  • Login

How to delete text after the 2nd specific character in each row

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
16 Posts 5 Posters 1.2k 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.
  • C
    Crimson Virtuoso
    last edited by Jun 18, 2020, 10:00 AM

    how to delete text after the 2nd “|” in each row ( i just showed 3 rows for example )

    string1 | string2 | string3 | string5744
    string4001 | string2668 | string1234 | string496
    string201 | string202 | string203 | string489

    I would like to change it into this one :

    string1 | string2 |
    string4001 | string2668 |
    string201 | string202 |

    E T 3 Replies Last reply Jun 18, 2020, 11:30 AM Reply Quote 0
    • E
      Ekopalypse @Crimson Virtuoso
      last edited by Jun 18, 2020, 11:30 AM

      @Crimson-Virtuoso said in How to delete text after the 2nd specific character in each row:

      how to delete text after the 2nd “|” in each row ( i just showed 3 rows for example )
      string1 | string2 | string3 | string5744
      string4001 | string2668 | string1234 | string496
      string201 | string202 | string203 | string489
      I would like to change it into this one :
      string1 | string2 |
      string4001 | string2668 |
      string201 | string202 |

      One solution might be to find: ^(.*?\|){2}\K.*
      and keep replace empty. Check regular expression.

      1 Reply Last reply Reply Quote 3
      • C
        Crimson Virtuoso
        last edited by Jun 18, 2020, 11:56 AM

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • E
          Ekopalypse @Crimson Virtuoso
          last edited by Jun 18, 2020, 12:17 PM

          @Crimson-Virtuoso

          one thing I keep forgetting, when using the \K notation then
          you need to do replace all in order to make it work.
          You can use find to jump through the matches but you cannot use
          replace while doing this. To be honest, I don’t know why this happens. Bug/Feature !?

          A T 2 Replies Last reply Jun 18, 2020, 12:44 PM Reply Quote 0
          • A
            Alan Kilborn @Ekopalypse
            last edited by Jun 18, 2020, 12:44 PM

            @Ekopalypse

            you need to do replace all in order to make it work.
            To be honest, I don’t know why this happens. Bug/Feature !?

            What if it isn’t really a bug and isn’t really a feature?
            But just something that happens as a side effect of usual processing?
            I guess it has to be labelled a “bug”.

            See HERE for the line of code that I think prevents single replace (with \K involved) from working.

            When the Replace button is pressed, it really performs a “find next” first. The match is shown to the user in the form of a selection. A second press of Replace will then do the replacement, but first it verifies by doing another “find next” that the selected text is still a match. The user could certainly have changed the selection to something else in the meantime, and N++ doesn’t just want to “blindly” replace the selected text.

            In the case of a \K usage, what is shown to the user (via selected text) as a match is only what matches after the (final) \K in the search expression. Thus when N++ does its “verify check” that the selected text is indeed OK to be replaced, that check fails because it doesn’t match the entire search expression, but rather only the trailing part. The line of code I flagged above will actually be comparing with the next real match of the entire expression, which fails and thus the replacement is not conducted.

            Replace All with \K works fine because it doesn’t have the “burden” of needing a mechanism to show what matched to the user.

            E 1 Reply Last reply Jun 18, 2020, 1:15 PM Reply Quote 3
            • E
              Ekopalypse @Alan Kilborn
              last edited by Jun 18, 2020, 1:15 PM

              @Alan-Kilborn

              thx for clarification, sounds reasonable.

              1 Reply Last reply Reply Quote 0
              • G
                guy038
                last edited by Jun 18, 2020, 8:46 PM

                Hi, @crimson-virtuoso, @alan-kilborn, @ekopalypse and All,

                Alan, very informative post, indeed ! So, If I correctly understand what you said, this means that :

                • If the test, on present line 1816 of the FindReplaceDlg.cpp file, for a possible change of selection range, would have been absent, the \K regex feature would work, while doing a single replace operation, with the Replace button ?

                I backup your post’s link for future reference, if any question regarding the weird behavior of the \K feature ;-))

                Best Regards,

                guy0038

                A 1 Reply Last reply Jun 18, 2020, 8:56 PM Reply Quote 1
                • A
                  Alan Kilborn @guy038
                  last edited by Jun 18, 2020, 8:56 PM

                  @guy038

                  Well, it wouldn’t be as simple as removing the indicated line, as that would break other functionality.
                  Truly, though, I haven’t thought too much about it.
                  While I can kinda read C++, and I can use the Visual Studio debugger to step through its code, it isn’t my strong suit.
                  Hmm, maybe I don’t have a “strong suit”. (Is that a known expression?)
                  I leave it to others, but I’m fairly confident in my analysis presented earlier about this; I spent a bit of time single-stepping through it and looking at it.

                  1 Reply Last reply Reply Quote 2
                  • T
                    Thomas 2020 @Ekopalypse
                    last edited by Jul 31, 2020, 11:20 AM

                    @Ekopalypse said in How to delete text after the 2nd specific character in each row:

                    You can use find to jump through the matches but you cannot use
                    replace while doing this.

                    I can’t think of any better.
                    It would be better to replace “^” with “\A”.

                    ^((\w+ \| ){2})(.+)
                    \1
                    
                    E 1 Reply Last reply Jul 31, 2020, 11:26 AM Reply Quote 0
                    • E
                      Ekopalypse @Thomas 2020
                      last edited by Jul 31, 2020, 11:26 AM

                      @Pan-Jan said in How to delete text after the 2nd specific character in each row:

                      I can’t think of any better.
                      It would be better to replace “^” with “\A”.

                      Why do you think so? What is the advantage of using \A instead of ^ in this case?

                      1 Reply Last reply Reply Quote 0
                      • T
                        Thomas 2020
                        last edited by Jul 31, 2020, 12:44 PM

                        If I wanted to swap lines one by one
                        the pattern works from the cursor position.

                        E 1 Reply Last reply Jul 31, 2020, 12:59 PM Reply Quote 0
                        • E
                          Ekopalypse @Thomas 2020
                          last edited by Jul 31, 2020, 12:59 PM

                          @Pan-Jan

                          the pattern works from the cursor position.

                          that is correct but this means also that it can match the wrong parts.
                          Assume the following ( | this should be the caret)

                          string1 | stri|ng2 | string3 | string5744

                          in that case it would handle string5744 as the match and NOT string3 | string5744

                          1 Reply Last reply Reply Quote 1
                          • T
                            Thomas 2020 @Crimson Virtuoso
                            last edited by Jul 31, 2020, 2:52 PM

                            @Crimson-Virtuoso said in How to delete text after the 2nd specific character in each row:

                            string1 | string2 | string3 | string5744
                            string4001 | string2668 | string1234 | string496
                            string201 | string202 | string203 | string489

                            in this example there are 3 x |

                            E 1 Reply Last reply Jul 31, 2020, 3:12 PM Reply Quote 0
                            • E
                              Ekopalypse @Thomas 2020
                              last edited by Jul 31, 2020, 3:12 PM

                              @Pan-Jan

                              I’m sorry, but I think I’m out.
                              If you think you’re gonna get anywhere with the way you communicate, good luck with that.
                              I won’t waste any more time trying to figure out what you might mean.

                              1 Reply Last reply Reply Quote 1
                              • T
                                Thomas 2020
                                last edited by Jul 31, 2020, 5:47 PM

                                Schowek03.jpg

                                1 Reply Last reply Reply Quote -1
                                • T
                                  Thomas 2020
                                  last edited by Jul 31, 2020, 6:42 PM

                                  Schowek04.jpg

                                  ^((\w+ \| ){2})(.+)
                                  

                                  can be replaced individually

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