import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String s_lf = "this tis a fine #line1\nthis tis another fine #line2\nthis_belongs_to abobe line\nthis tis still is OK #line4";
		String s_crlf = "this tis a fine #line1\r\nthis tis another fine #line2\r\nthis_belongs_to abobe line\r\nthis tis still is OK #line4";
		
		System.out.println(s_lf.replaceAll("\\R(?!.*#)", "")); 
		System.out.println(s_crlf.replaceAll("\\R(?!.*#)", ""));
		
		System.out.println(s_lf.replaceAll("(?m)\\R(?=[^\n#]+$)", "")); 
		System.out.println(s_crlf.replaceAll("(?m)\\R(?=[^\n#]+$)", "")); 
	}
}