Community
    • Login

    Delete all text before a string

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 7.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.
    • G
      guszn10mm
      last edited by

      Hello, I am trying to delete all text in a file before the first instance of a string. There are multiple instances of the string, but I need to essentially find the first occurrence of the string, and delete all text preceding the string.

      As an example, the file is:

      aaa
      bbb
      ccc
      111
      222
      333
      2021-05-26abcdefg
      ddd
      444
      123456789
      2021-05-26

      I need the locate the FIRST occurrence of the string - “2021-05-26” and delete all text before that string, NOT including “abcdefg” on the same line. So I would need to delete the lines, aaa | bbb | ccc | 111 | 222 | 333

      The desired output being:

      2021-05-26abcdefg
      ddd
      444
      123456789
      2021-05-26

      I have been looking for a regex command for this, but haven’t had any luck so far. Any assistance is appreciated.

      Notepad++ v8.1.9.2 (32-bit)
      Build time : Nov 21 2021 - 04:27:12
      Path : C:\Program Files (x86)\Notepad++\notepad++.exe
      Admin mode : OFF
      Local Conf mode : OFF
      Cloud Config : OFF
      OS Name : Windows 10 Enterprise (64-bit)
      OS Version : 2009
      OS Build : 19042.1415
      Current ANSI codepage : 1252
      Plugins : mimeTools.dll NppConverter.dll NppExport.dll

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @guszn10mm
        last edited by

        @guszn10mm

        FIND = (?s)\A.*?(2021-05-26)

        • (?s) will allow . to match newline (or you can set the flag in the dialog box)
        • \A represents the start of the file
        • .*? says “0 or more, as few as possible”
        • (...) puts whatever’s in the parens in a group
        • 2021-05-26 literal string you want to match

        REPLACE = $1

        • $1 refers to the contents of the first group from the FIND

        SEARCH MODE = regular expression

        Do the replacement

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • FAQ: Where to find regular expressions (regex) documentation
        • Notepad++ Online User Manual: Searching/Regex
        1 Reply Last reply Reply Quote 3
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors