문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top