Arnav
Arnav

Reputation: 51

Extracting String from HTML source

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

Upvotes: 1

Views: 160

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

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!

Upvotes: 2

Related Questions