1

Hi want to extract String between HTML Tags from a source code but I am getting an error by using the code given below. Could someone help me with the reason for error?

Pattern pattern = Pattern.compile("/\<body[^>]*\>([^]*)\<\/body/");
Matcher matcher = pattern.matcher(s1);
while (matcher.find()) {
  System.out.println( "Found value: " + matcher.group(1).trim() );
}

The error I am getting is: "Invalid escape sequence"

Thanks

1

1 Answer 1

2

Don't parse html files with regex. I suggest you to use jsoup parser.

String html = "<html><body><h1> Hello, World! </h1></body></html>";
Document doc = Jsoup.parse(html);
String text = doc.body().text();
System.out.println(text);

Output:

Hello, World!
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Avinash. Appreciate it
Since you're new to StackOverflow, i suggest you to read this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.