• Login
Community
  • Login

How to replace a whole line starting with a given expression

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 4 Posters 31.8k 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.
  • R
    Rachad Antonius
    last edited by Dec 28, 2017, 4:15 AM

    Hello
    I would like to remove all lines in a NotePad++ text file starting with a given word.
    These lines may vary in the number of caracters. Exemple : I want to remove all lines starting with

    X-Gmail-Labels: (and then, the length of the line as well as its content may vary)
    Such as :
    X-Gmail-Labels: =?UTF-8?Q?Messages_envoy=C3=A9s,Bo=C3=AEte_de_r=C3=A9ception?=

    What to I write in the Search box ?
    Thanks !
    Rachad

    C 1 Reply Last reply Dec 28, 2017, 12:34 PM Reply Quote 0
    • C
      Claudia Frank @Rachad Antonius
      last edited by Dec 28, 2017, 12:34 PM

      @Rachad-Antonius

      by using regular expression and

      /s*GIVEN_WORD.*
      

      in find what, keep replace with empty you can replace all lines.

      For details about the regex see here .

      Cheers
      Claudia

      S 1 Reply Last reply Dec 28, 2017, 2:08 PM Reply Quote 0
      • S
        Scott Sumner @Claudia Frank
        last edited by Dec 28, 2017, 2:08 PM

        @Claudia-Frank

        Ok, Claudia, this time you have really lost me. I don’t understand your solution! :-(

        Here’s mine:

        Find-what zone: (?-s)^X-Gmail-Labels:.*\R
        Replace-with zone: make sure this is EMPTY
        Search-mode: Regular Expression
        Wrap-around checkbox: ticked
        Action: Press Replace-All button

        That is tailored to your example, Rachad. Obviously(?) the general solution is:

        Find-what zone: (?-s)^givenword.*\R
        (all else the same)

        C 1 Reply Last reply Dec 28, 2017, 2:17 PM Reply Quote 3
        • C
          Claudia Frank @Scott Sumner
          last edited by Claudia Frank Dec 28, 2017, 2:20 PM Dec 28, 2017, 2:17 PM

          @Scott-Sumner

          Hi Scott,
          I’m puzzled too, where is the circumflex and why is there a forwardslash instead of a backslash??

          I thought I wrote

          ^\s*GIVEN_WORD.*
          

          Strange.
          I added the \s just in case it is in front of GIVEN_WORD - might be not exactly what OP is asking for.

          Thank you for pointing this out.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 2
          • G
            guy038
            last edited by guy038 Dec 28, 2017, 9:46 PM Dec 28, 2017, 9:28 PM

            Hello @rachad-antonius, @claudia-frank, @Scott-sumner, and All,

            Claudia, don’t forget that the \s regular syntax matches any single character of the range [\t\n\x0B\f\r\x20\x85\xA0\x{2028}\x{2029}]

            So let’s imagine this initial text, below :

            • Which begins with 3 empty lines

            • With 2 other empty lines, after line bbbbbbb

            • And the second to last line contains, ONLY the expression GIVEN_WORD

            
            
            
            GIVEN_WORD aaaaaaaaaaaa
            bbbbbbb
            
            
            GIVEN_WORD ccccccccc
            GIVEN_WORDeeeeeeeeeeeeeeeee
            GIVEN_WORD fffffff
            gggggggggggggggggg
            hhhhhhhhh
            GIVEN_WORD iiiiiiiiiiiiiiiiiiiiiiiiiiiii
            GIVEN_WORD
            jjjjjjjjjjjjjjjjjjjjjj
            

            With your regex, ^\s*GIVEN_WORD.*, this text turns into :

            
            bbbbbbb
            
            gggggggggggggggggg
            hhhhhhhhh
            
            jjjjjjjjjjjjjjjjjjjjjj
            

            Whereas the Scott’s regex, (?-s)^GIVEN_WORD.*\R gives :

            
            
            
            bbbbbbb
            
            
            gggggggggggggggggg
            hhhhhhhhh
            jjjjjjjjjjjjjjjjjjjjjj
            

            This later syntax seems more correct as it matches a single line, at a time ;-)) doesn’t it ? Claudia, you may click several times on the couple Find Next | Replace, with the View > Show Symbol > Show All Characters option set !

            BTW, @rachad-antonius, the \R syntax, at the end of the regex, matches the line-break characters of the current line, whatever the kind of file ( Unix, Windows, or Mac ) !


            Additional Notes :

            • In case, @rachad-antonius, that you would like, in addition, to delete entire lines, having some blank characters ( spaces and/or tabulations ), before the GIVEN_WORD, you could use the regex ^\h*GIVEN_WORD(?-s).*\R

            • Secondly , depending if you prefer the GIVEN_WORD to be matched with that exact case, or not, you could choose :

              • The regex ^\h*(?-i)GIVEN_WORD(?-s).*\R for an exact case match

              • The regex ^\h*(?i)GIVEN_WORD(?-s).*\R for a match, independent of the case of GIVEN_WORD

            • Finally, if GIVEN_WORD represents an complete expression, containing special regex characters, I advice you to use the following one : ^\h*(?-i)\QGIVEN_WORD\E(?-s).*\R. Indeed, between the 2 syntaxes \Q and \E, you may insert any character or symbol, without no restriction !!

            Refer to :

            http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html#boost_regex.syntax.perl_syntax.quoting_escape


            For instance, if you want to delete the entire lines, beginning with the string +++ | Test | +++, whatever the case of the word test, and possibly preceded, in the same line, by some blank characters, use the regex, below :

            ^\h*(?i)\Q+++ | Test | +++\E(?-s).*\R

            Remark :

            If you don’t use the quoted form \Q.......\E, the regex must be rewritten as below, which each special regex character escaped with an \ character !

            ^\h*(?i)\+\+\+ \| Test \| \+\+\+(?-s).*\R

            Best Regards,

            guy038

            P. S. :

            Claudia, many Thanks for your Xmas Greetings ! Sorry for not replying, in time :-< I just forgot to consult my e-mail box ;-))

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