• Login
Community
  • Login

Delete text up to a certain number of characters from the end of a line

Scheduled Pinned Locked Moved General Discussion
3 Posts 2 Posters 2.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.
  • K
    Keith Wood
    last edited by Nov 12, 2020, 4:35 PM

    I’ve tried a few different regex strings, but can’t figure out the lookahead part to make this work. I have a few thousand lines of text and am trying to remove the variable number of characters and leave a fixed number of them from the end of the line. Examples:

    Input:

    alhsoidoli aoisudoifu oaisoi
    iodaoiu a;osidh ;oaiyu ;oisdlhidadsli
    LOIHYoihdlhi OIHlohi LOHIOOLhiol oo

    Output:

    ifu oaisoi
    dlhidadsli
    OOLhiol oo

    So the find criteria would match the last 10 characters from the end and preserve that, deleting the entire rest of the line. I don’t have any special delimiting character I can match on; every line is very different. The only thing I do know is the fixed number of characters from the end of the line. Thanks!

    P 1 Reply Last reply Nov 12, 2020, 4:42 PM Reply Quote 0
    • P
      PeterJones @Keith Wood
      last edited by PeterJones Nov 12, 2020, 4:43 PM Nov 12, 2020, 4:42 PM

      @Keith-Wood,

      My logic would be “capture from the beginning of the line, as many characters as possible, until a lookahead of 10 characters before the EOL is matched”. That translates into something like:

      • FIND = ^.*(?=.{10}$)
        REPLACE = empty
        MODE = regular expression

      Which worked for me

      ifu oaisoi
      dlhidadsli
      OOLhiol oo
      

      If this doesn’t work for you, you will have to clarify, after reading and taking to heart the following advice:

      ----

      Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

      1 Reply Last reply Reply Quote 5
      • K
        Keith Wood
        last edited by Nov 12, 2020, 8:14 PM

        That worked like a champ! Thank you ever so much!

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