Regex: delete multiple blank lines


I am trying to process a logfile. The log file is interspersed with arbitrary numbers of blank lines, e.g.





Can someone help me with a regex that will delete all these blank lines. My thinking so far has lead me to using '^$' to match the blank line rather than \
. But I'm not sure what to replace the blank line with to delete it. I don't want to use a blank line obviously, nor to I want to use a space character - I want to remove the white space all together.

Any help appreciated. Thanks.


Replace it with an empty string. Actually, str_replace() should do the job:

$text = str_repace("\
", '', $text);

That's what I thought would work. Perhaps I should confess that I'm actually doing this in textpad - not PHP blush, but I assumed the regex would be the same. If I replace the \
with an empty string in textpad, it replaces the blank lines, but also the newline at the end of each valid entry - effectively concatenating all the entries onto a single line. I would like to retain the newline at the end of a valid entry. I.e. if I have ^<<any text>>$\
that should be kept, but if I have ^$ that should be removed. Hope this makes sense.


How about:

$text = preg_replace('/[\\r\
]+/m', "\
", $text);

No luck with that one either Salathe, but thanks. I need to use a textpad because I don't have access to a unix environment or a php environment at the moment. Instead, I have to use the regex search/replace feature that textpad has.

Out of interest, are you in Edinburgh US or Edinburgh, Scotland? Never met anyone in the forums from Edinburgh, Scotland!


After a cup of coffee, the answer hit me - it's amazing what caffiene can do!

I searched for ^$\
and replaced it with an empty string, this removed all of the blank lines but left the remaining new lines on valid entries untouched. It's quite obvious when I think about it... ^$\
, i.e. the start of a line, immediately followed by the end of a line, followed by a newline character! D'oh!

Cheers for your input folks!

closed #7