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>

Share:
10,413
Mark Timothy
Author by

Mark Timothy

Updated on June 15, 2022

Comments

  • Mark Timothy
    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
    Mark Timothy about 9 years
    please post the complete regex expression
  • karthik manchala
    karthik manchala about 9 years
    @user3264864 use the above expression
  • Mark Timothy
    Mark Timothy about 9 years
    invalid escape sequence
  • Saif
    Saif about 9 years
    use double back slash \\w and \\W @user3264864
  • Bhaskara Arani
    Bhaskara Arani over 5 years
    <style[\\w\\W]+?<\\/style\\> It will do the job
  • bmiller
    bmiller almost 4 years
    I 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
    risingStark over 2 years
    Thanks very much, it helped me a lot.