• Login
Community
  • Login

Delete last words after : at the end of each line

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 5.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.
  • V
    Vinc Cser
    last edited by Apr 12, 2019, 4:57 PM

    Hello, i have a txt with 1000 lines each like this
    happylion3872:PIFMYJD8:9kjrgmdsjf4vxlc1dkc791fsd7f878
    and i want to replace all 1000 lines to only be this
    happylion3872:PIFMYJD8

    How do i do that? thanks for help in advance

    1 Reply Last reply Reply Quote 0
    • P
      PeterJones
      last edited by Apr 12, 2019, 5:03 PM

      @Vinc-Cser, welcome to the Notepad++ Community

      You asked:

      How do i do that? thanks for help in advance

      Assuming you just want to delete the last colon and everything following,

      • find = :[^:]*$
      • replace = empty
      • mode = regular expression
      V 1 Reply Last reply Apr 12, 2019, 5:18 PM Reply Quote 2
      • P
        PeterJones
        last edited by Apr 12, 2019, 5:04 PM

        @Vinc-Cser
        FYI: here is some helpful information for finding out more about regular expressions, and for formatting posts in this forum (especially quoting data) so that we can fully understand what you’re trying to ask:

        This forum is formatted using Markdown , with a help link buried on the little grey ? in the COMPOSE window/pane when writing your post. For more about how to use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic , and my updates near the end . It is very important that you use these formatting tips – using single backtick marks around small snippets, and using code-quoting for pasting multiple lines from your example data files – because otherwise, the forum will change normal quotes ("") to curly “smart” quotes (“”), will change hyphens to dashes, will sometimes hide asterisks (or if your text is c:\folder\*.txt, it will show up as c:\folder*.txt, missing the backslash). If you want to clearly communicate your text data to us, you need to properly format it.

        If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

        Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

        1 Reply Last reply Reply Quote 1
        • V
          Vinc Cser @PeterJones
          last edited by Apr 12, 2019, 5:18 PM

          @PeterJones said:

          @Vinc-Cser, welcome to the Notepad++ Community

          You asked:

          How do i do that? thanks for help in advance

          Assuming you just want to delete the last colon and everything following,

          • find = :[^:]*$
          • replace = empty
          • mode = regular expression

          worked, thanks

          1 Reply Last reply Reply Quote 2
          • G
            guy038
            last edited by guy038 Apr 12, 2019, 5:43 PM Apr 12, 2019, 5:41 PM

            Hello @vinc-cser, @Peterjones, and All,

            Peter, you beat me to it by a few minutes ;-)). I was thinking about these two solutions :

            SEARCH (?-s)^(.+):.+

            REPLACE \1

            OR :

            SEARCH :[^:\r\n]+$

            REPLACE Leave EMPTY

            Note that your solution :[^:]*$ does not work well when next line(s) does not contain any colon. For instance :

            happylion3872:PIFMYJD8:9kjrgmdsjf4vxlc1dkc791fsd7f878
            happylion3872:PIFMYJD8:9kjrgmdsjf4vxlc1dkc791fsd7f878
            Some text
            to see
            the problem
            happylion3872:PIFMYJD8:9kjrgmdsjf4vxlc1dkc791fsd7f878
            

            So, to prevent grabbing multiple lines , use either :

            • My second solution :[^:\r\n]+$ , which does not accept End of line chars before an end of line

            • Your solution, slightly modified, :[^:]*?$ , which looks for the nearest “end of line”  location !


            @vinc-cser, beware that running twice, any of these regex S/R, would delete the part :PIFMYJD8, as well !

            Best Regards

            guy038

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