Community
    • Login

    How to exclude empty lines containing tabs and spaces from the search?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 3 Posters 2.4k 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.
    • andrecool-68A
      andrecool-68
      last edited by

      Need to find the beginning and end of the line.
      Add quotation marks to the beginning and end of the string.

      Find what : (^|$)

      Replace with :"

      How to exclude empty lines containing tabs and spaces from the search?

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

        @andrecool-68 ,

        You’ve been here long enough to know how to paste in example before and after data, and show what you tried. Please do so. This will be the last time I answer you when you haven’t showed any effort in your question.

        Before:

        Something Here
        Something There
        
        There was a blank line
        

        After:

        "Something Here"
        "Something There"
        
        "There was a blank line"
        

        This is a pretty simple one: you just want to select beginning, anything, require at least one non-space, anything, end of line. Translate those into the regular expression syntax.

        • Find: ^.*[\S]+.*$
        • Replace: "$0"

        (That works whether the blank line is blank or whether it contains one or more spaces or tabs)

        1 Reply Last reply Reply Quote 3
        • andrecool-68A
          andrecool-68
          last edited by

          @PeterJones
          I greet you!

          I copied your example and it does not work for me. Added only two quotes, first and end of the file))

          "Something Here
          Something There
          
          There was a blank line"
          
          1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones
            last edited by PeterJones

            Turn off . matches newline, or change find to (?-s)^.*[\S]+.*$, where (?-s) overrides the . matches newline setting.

            1 Reply Last reply Reply Quote 2
            • guy038G
              guy038
              last edited by

              Hi, @andrecool-68, @peterjones, and All,

              An other solution could be :

              SEARCH : (?-s)^.+

              REPLACE : "$0"

              Notes :

              • As usual, the in-line modifier (?-s) means that any further dot (.) character will refer to a single standard character, only ( Not an EOL one )

              • So, the part ^.+ select the largest non-null range of standard characters, ( i.e. all contents of current line, without its line-break )

              • In replacement, we just re-write the entire match area ( $0), surrounded with two double quotes

              Best Regards,

              guy038

              1 Reply Last reply Reply Quote 3
              • PeterJonesP
                PeterJones
                last edited by

                @guy038 ,

                My quibble with that solution is that the original question was “How to exclude empty lines containing tabs and spaces from the search?” It seems to me that @andrecool-68 didn’t want quotes around lines that had only whitespace, but your regex will:

                1 Reply Last reply Reply Quote 2
                • andrecool-68A
                  andrecool-68
                  last edited by

                  @PeterJones
                  Following your advice now both your examples work!
                  Many thanks to all for the help!

                  1 Reply Last reply Reply Quote 2
                  • guy038G
                    guy038
                    last edited by guy038

                    Hi, @andrecool-68, @peterjones, and All,

                    Oh, my bad, Peter, you’re right :-(( I should have read more carefully !

                    So, my previous regex must be changed, as below :

                    SEARCH : (?-s)^(?=.*\S).+

                    REPLACE "$0"

                    Notes :

                    • This new search regex looks, as before, for all contents of any non-empty line ^.+ but, this time, a match occurs ONLY IF the look-ahead (?=.*\S) is true, i.e. if, from beginning of line, a non-space char can be found, further on, in current line

                    • However, note that this regex S/R also preserves blank chars beginning or ending text of any line !


                    So to simply surround the text with double quotes, ignoring the white characters that start and/or end that text, here is a second regex S/R :

                    SEARCH ^\h*(\S+(\h+\S+)*)\h*

                    REPLACE "\1"

                    Notes :

                    • The part \S+(\h+\S+)* represents any text, made of one or several words, even containing non-word characters ( for instance #, @… ) , separated by, at least, one horizontal blank char

                    • This part is surrounded with parentheses, so stored as group 1 and surrounded itself with \h*, which stands for possible leading or ending horizontal blank characters

                    Remark : If you change the Replace regex, of this second S/R with "$0", it’s, again, equivalent to the first regex S/R of that post !

                    Best Regards

                    guy038

                    P.S. : You say what ? Writing “Best Regards”, at the end of this post, I first began to write : “Best Regex…” ! My god, I’m definitively addicted ;-))

                    1 Reply Last reply Reply Quote 3
                    • andrecool-68A
                      andrecool-68
                      last edited by

                      @guy038 Hi!
                      Your option works! Thank you very much for your help!

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