• Login
Community
  • Login

How to delete text after a specific character in each row

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
3 Posts 3 Posters 20.5k 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.
  • R
    Rana Jawad
    last edited by Jul 13, 2017, 11:37 PM

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

    S 1 Reply Last reply Jul 14, 2017, 1:22 AM Reply Quote 0
    • S
      Scott Sumner @Rana Jawad
      last edited by Jul 14, 2017, 1:22 AM

      @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
      • G
        guy038
        last edited by guy038 Jul 14, 2017, 6:43 AM Jul 14, 2017, 6:41 AM

        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
        2 out of 3
        • First post
          2/3
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors