使用正则表达式从文本文件中删除样式标签

Mar*_*thy 4 java regex

我需要从文本文件中删除样式标签。

我尝试了以下代码

String text = readFile("E:/textwithstyletags.txt");
retVal = text.replaceAll("<style(.+?)</style>", "");
Run Code Online (Sandbox Code Playgroud)

当文本文件具有没有新行的样式标签时,即可以使用 <style> body{ color:red; } </style>

像这样有新行时不起作用

<style> 
body{ 
color:red; 
} 
</style>
Run Code Online (Sandbox Code Playgroud)

Jar*_*ler 6

regex101 上测试。

图案:

<style((.|\n|\r)*?)<\/style>    
Run Code Online (Sandbox Code Playgroud)

您的代码:

String text = readFile("E:/textwithstyletags.txt");
retVal = text.replaceAll("<style((.|\\n|\\r)*?)<\\/style>", "");
Run Code Online (Sandbox Code Playgroud)


kar*_*ala 5

您可以使用[\s\S]代替.你的正则表达式

即:

retVal = text.replaceAll("<style([\\s\\S]+?)</style>", "");
Run Code Online (Sandbox Code Playgroud)