Community
    • Login

    How to delete text after a specific character in each row

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 21.0k Views 2 Watching
    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.
    • Rana JawadR Offline
      Rana Jawad
      last edited by

      how to delete text after | in each row ( i just showed 3 rows for example )
      string1 | string2
      string4001 | string2668
      string201 | string202

      Scott SumnerS 1 Reply Last reply Reply Quote 0
      • Scott SumnerS Offline
        Scott Sumner @Rana Jawad
        last edited by

        @Rana-Jawad

        Do a regular expression replacement:

        Find-what box: ^([^|]+\|).+
        Replace-with box: \1
        Search mode: ☑ Regular expression -AND- ☐ . matches newline

        After doing this to your data, it results in:

        string1 |
        string4001 |
        string201 |
        

        The ^ in the regular expression anchors the search to the start of each line. Next one or more non-| characters are matched followed by a |. The characters on the line up to this point are remembered as group #1 by the enclosing parentheses. After this any number of characters is matched out to the end of the line. At replace time, the contents of group #1 remembered before are substituted for the entire line.

        There are a lot of ways to do this with regular expressions. This is just one way.

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

          Hello, @Rana-jawad,

          An equivalent regex to the Scott’s one would be :

          SEARCH (?-s)(?<=\|).*

          REPLACE Leave EMPTY !

          Notes :

          • The first part (?-s) is a modifier, which forces the regex engine to interpret the dot regex character as standing for any single standard character ( not End of Line characters )

          • The last part .* tries to match any range, even empty, of standard characters

          • The middle part (?<=\|) is a positive look-behind, that is to say a condition which must be true for an overall match

          • This condition implies that the matched range of characters must be preceded by a literal vertical bar ( | ), which have to be escaped with an \, because the | character is the regex symbol for alternation

          • And, due to the empty replacement box, this range of characters, located after the | character, is simply deleted


          However, just note that, with my regex or Scott regex, if some of your lines contains several | characters, any text after the first | character, will be deleted. For instance, the text, below :

          string1 | string2 | string3
          string4001 | string2668 | string1234
          string201 | string202 | string203
          

          would be changed into this one :

          string1 |
          string4001 |
          string201 |
          

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors