• Login
Community
  • Login

Notepad++ How to delete sections of text starting with a line containing a certain phrase

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 1.6k 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.
  • B
    Banjo G
    last edited by May 4, 2020, 2:07 PM

    I’m trying to edit a few calibre files that have tags attached, but the tag line is not always formatted the same.

    Eg.

    div class=“pcalibre1 pcalibre2 pcalibre tags-list”

    div class=“pcalibre1 pcalibre2 tags-list pcalibre”.

    I want to delete everything including and between the lines containing tags-list and entry-speaker.

    Is there an easy way to do this with regex?

    A 1 Reply Last reply May 4, 2020, 2:20 PM Reply Quote 0
    • A
      Alan Kilborn @Banjo G
      last edited by Alan Kilborn May 4, 2020, 2:21 PM May 4, 2020, 2:20 PM

      @Banjo-G said in Notepad++ How to delete sections of text starting with a line containing a certain phrase:

      delete everything including and between the lines containing tags-list and entry-speaker

      Your example doesn’t show any entry-speaker so I suggest you have a look HERE.

      B 1 Reply Last reply May 4, 2020, 2:27 PM Reply Quote 1
      • B
        Banjo G @Alan Kilborn
        last edited by May 4, 2020, 2:27 PM

        @Alan-Kilborn Sorry, those were examples of the lines not being formatted the same.
        An better example would be

        <div class=“pcalibre2 pcalibre1 pcalibre tags-list”>
        …
        …
        …
        …
        …
        …
        …
        <h4 class=“pcalibre2 pcalibre1 pcalibre entry-speaker”>

        With all lines wanting to be deleted. The problem is that the tags-list and entry-speaker lines are often scrambled.

        Eg.
        <div class=“pcalibre2 pcalibre tags-list pcalibre1”>
        <div class=“pcalibre1 pcalibre2 tags-list pcalibre”>
        <div class=“pcalibre1 pcalibre2 pcalibre tags-list”>
        <div class=“pcalibre2 pcalibre1 pcalibre tags-list”>

        1 Reply Last reply Reply Quote 0
        • G
          guy038
          last edited by guy038 May 5, 2020, 9:00 AM May 4, 2020, 11:48 PM

          Hello, @banjo-g, @alan-kilborn and All,

          I think you could test this regex S/R, below :

          SEARCH (?-s)^.+?tags-list(?s).+?entry-speaker.+?$\R

          REPLACE Leave EMPTY

          against this sample text :

          blabla
          bhahblah
          blabla
          <div class=“pcalibre2 pcalibre1 pcalibre tags-list”>
          …
          …
          …
          …
          <h4 class=“pcalibre2 entry-speaker pcalibre1 pcalibre”>
          blabla
          bhahblah
          blabla
          <div class=“pcalibre1 tags-list pcalibre2 pcalibre”>
          …
          …
          …
          …
          <h4 class=“pcalibre2 pcalibre1 pcalibre entry-speaker”>
          blabla
          bhahblah
          blabla
          <div class=“pcalibre2 pcalibre tags-list pcalibre1”>
          …
          …
          …
          …
          <h4 class=“entry-speaker pcalibre2 pcalibre1 pcalibre”>
          blabla
          bhahblah
          blabla
          rt
          <div class=“tags-list pcalibre1 pcalibre2 pcalibre”>
          …
          …
          …
          …
          <h4 class=“pcalibre2 pcalibre1 entry-speaker pcalibre”>
          blabla
          bhahblah
          blabla
          

          should be OK ;-))

          Best Regards,

          guy038

          B 1 Reply Last reply May 5, 2020, 4:32 AM Reply Quote 2
          • B
            Banjo G @guy038
            last edited by May 5, 2020, 4:32 AM

            @guy038 That works wonders, thanks!

            1 Reply Last reply Reply Quote 0
            • G
              guy038
              last edited by guy038 May 5, 2020, 7:14 PM May 5, 2020, 9:04 AM

              Hi, @banjo-g, @alan-kilborn and All,

              A generic and general form of the regex, described in my previous post, could be :

              SEARCH (?-is)^.*?Expression A(?s).*?Expression B.*?$\R

              Basically, this regex :

              • Searches for two lines :

                • A line A, containing Expression A, at any location of line A

                • A line B, containing Expression B, at any location of line B

              • Selects all range of characters, generally multi-lines, from the beginning of line A till the end of line B, with its EOL characters

              • The lines A and B may be identical. However, in that case, Expression B must be located after Expression A, in current line !

              Notes :

              • First, the in-line modifier (?-is)

                • Carries a non-insensitive search ( so sensitive to case )

                • Forces the regex engine to interpret the regex dot symbol . as matching a single standard character ( not EOL ones )

              • Then, the part ^.*?Expression A matches, from beginnning of line, the shortest range, possibly null, of standard characters, followed by Expression A, with that exact case

              • Now, the part (?s).*?Expression B looks for the shortest range, possibly null, of characters, including EOL, followed by Expression B, with that exact case

              • Finally, the part .*?$\R searches for the shortest range, possibly null, of characters till an end of line, followed with its line-break

              Cheers,

              guy038

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