Community
    • 登入

    How to delete text after a specific character in each row

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    3 貼文 3 Posters 20.5k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Rana JawadR
      Rana Jawad
      最後由 編輯

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

      Scott SumnerS 1 條回覆 最後回覆 回覆 引用 0
      • Scott SumnerS
        Scott Sumner @Rana Jawad
        最後由 編輯

        @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 條回覆 最後回覆 回覆 引用 0
        • guy038G
          guy038
          最後由 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 條回覆 最後回覆 回覆 引用 0
          • 第一個貼文
            最後的貼文
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors