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



  • 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?



  • @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)



  • @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"


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



  • 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



  • @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:



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



  • 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 ;-))



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


Log in to reply