I need to remove style tags from text file..
I tried the following code
String text = readFile("E:/textwithstyletags.txt");
retVal = text.replaceAll("<style(.+?)</style>", "");
it works when the text file has style tags without new lines
i.e. <style> body{ color:red; } </style>
It doesn't work when there are new lines, like this
<style>
body{
color:red;
}
</style>
You can use [\s\S]
in place of .
in your regex
i.e:
retVal = text.replaceAll("<style([\\s\\S]+?)</style>", "");
Tested on regex101.
Pattern:
<style((.|\n|\r)*?)<\/style>
Your code:
String text = readFile("E:/textwithstyletags.txt");
retVal = text.replaceAll("<style((.|\\n|\\r)*?)<\\/style>", "");
Try this regex:
retVal = text.replaceAll("(?i)<style.*?>.*?</style>", "");
On a side note you can look at JSoup which is a java library made for HTML manipulation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With