• Login
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.
  • A
    andrecool-68
    last edited by May 6, 2019, 9:47 PM

    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
    • P
      PeterJones
      last edited by May 6, 2019, 10:01 PM

      @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
      • A
        andrecool-68
        last edited by May 6, 2019, 10:20 PM

        @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
        • P
          PeterJones
          last edited by PeterJones May 6, 2019, 10:23 PM May 6, 2019, 10:22 PM

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

          1 Reply Last reply Reply Quote 2
          • G
            guy038
            last edited by May 6, 2019, 10:40 PM

            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
            • P
              PeterJones
              last edited by May 7, 2019, 3:00 AM

              @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
              • A
                andrecool-68
                last edited by May 7, 2019, 7:46 AM

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

                1 Reply Last reply Reply Quote 2
                • G
                  guy038
                  last edited by guy038 May 7, 2019, 7:39 PM May 7, 2019, 7:36 PM

                  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
                  • A
                    andrecool-68
                    last edited by May 7, 2019, 7:57 PM

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

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