What could be the regular expression to match below String
String str = "<Element>\r\n <Sub>regular</Sub></Element>";
There is a
carriage return "\r", new line character "\n" and a space after <Element>.
My code is as below
if(str.matches("<Element>([\\s])<Sub>(.*)"))
{
System.out.println("Matches");
}
Use the "dot matches newline" switch:
if (str.matches("(?s)<Element>\\s*<Sub>(.*)"))
With the switch turned on, \s
will match newline characters.
I slso fixed your regex, removing two sets of redundant brackets, and adding the crucial *
after the whitespace regex.