Remove style tag from a text file with regex
10,413
Solution 1
You can use [\s\S]
in place of .
in your regex
i.e:
retVal = text.replaceAll("<style([\\s\\S]+?)</style>", "");
Solution 2
Tested on regex101.
Pattern:
<style((.|\n|\r)*?)<\/style>
Your code:
String text = readFile("E:/textwithstyletags.txt");
retVal = text.replaceAll("<style((.|\\n|\\r)*?)<\\/style>", "");
Solution 3
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.
Solution 4
you can use
this expression <style[\\w\\W]+?</style>
retVal = text.replaceAll("<style[\\w\\W]+?</style>", "");
It says to find all the alphanumeric character including the underscore(\w
) and not word (\W
) character till </script>
Author by
Mark Timothy
Updated on June 15, 2022Comments
-
Mark Timothy almost 2 years
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>
-
Mark Timothy about 9 yearsplease post the complete regex expression
-
karthik manchala about 9 years@user3264864 use the above expression
-
Mark Timothy about 9 yearsinvalid escape sequence
-
Saif about 9 yearsuse double back slash
\\w
and\\W
@user3264864 -
Bhaskara Arani over 5 years<style[\\w\\W]+?<\\/style\\> It will do the job
-
bmiller almost 4 yearsI like this, handles everything non-greedy and case insensitive, no muss no fuss. Although I think you need to add the s flag (dot matches all) to handle linefeeds between open & close tags. like:
retVal = text.replaceAll("(?is)<style.*?>.*?</style>", "");
-
risingStark over 2 yearsThanks very much, it helped me a lot.