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 text = "(a) last line % now with comment\n(b) last line with escaped \\%  and not treated\n(c) last line withouth any special chars\n(d) last line terminates with %\n(e) last line terminates with escaped \\%\n(f) beginning % but even escaped \\% is ignored";
		Pattern p = Pattern.compile("(?<!\\\\)(?:\\\\{2})*%[^\\\\%\r\n]*(?:\\\\[\\w\\W][^\\\\%\r\n]*)*\\z");
		Matcher m = p.matcher(text);
		if (m.find()) {
		  System.out.println("Match found!");
		}
	}
}