希望高手帮忙写一个正则表达式

PostX 2010-04-16 04:05:31
自己写了好久也没有写出来。想要从字符串中过滤掉下面内容
<style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>


使用 String.replaceAll(pattern,"")。谢谢
...全文
161 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2010-04-17
  • 打赏
  • 举报
回复
其实很简单,类似于匹配标签<...>使用
<[^>]*>
一样,表示匹配“<”开头,中间不是“>”的任意字符,一直匹配到“>”为止

<style>(?:(?!</?style\\b).)*</style>
就表示匹配“<style>”开头,中间不是“<style”或“</style”的任意字符,一直匹配到“</style>”为止

[^>] 排除的是任意无序字符,而(?!</?style\\b).排除的是一个有序的子串

(?i) 表示忽略大小写
(?s) 表示单行模式,使得小数点可以匹配任意字符
PostX 2010-04-17
  • 打赏
  • 举报
回复
特别是其中的?:以及\\b
PostX 2010-04-17
  • 打赏
  • 举报
回复
?:(?!</?style\\b)
这一段还不是很懂!嘿嘿,再麻烦一下呗
[Quote=引用 7 楼 lxcnn 的回复:]

其实很简单,类似于匹配标签<...>使用
<[^>]*>
一样,表示匹配“<”开头,中间不是“>”的任意字符,一直匹配到“>”为止

<style>(?:(?!</?style\\b).)*</style>
就表示匹配“<style>”开头,中间不是“<style”或“</style”的任意字符,一直匹配到“</style>”为止

[^>] 排除的是任意无序字符,而(?!</?s……
[/Quote]
PostX 2010-04-16
  • 打赏
  • 举报
回复
太佩服你了!!我写了一个下午,没写出来!!
你能解释一下你的pattern吗?

[Quote=引用 5 楼 lxcnn 的回复:]

这个意思?

Java code
String test = "自己写了好久也没有写出来。想要从字符串中过滤掉下面内容\n<style>\n<!--\n /* Font Definitions */\n @font-face\n{font-family:宋体;\npanose-1:2 1 6 0 3 1 1 1 1 1;}\n@font-face\n{font-family:宋体;\npa……
[/Quote]
-过客- 2010-04-16
  • 打赏
  • 举报
回复
这个意思?

String test = "自己写了好久也没有写出来。想要从字符串中过滤掉下面内容\n<style>\n<!--\n /* Font Definitions */\n @font-face\n{font-family:宋体;\npanose-1:2 1 6 0 3 1 1 1 1 1;}\n@font-face\n{font-family:宋体;\npanose-1:2 1 6 0 3 1 1 1 1 1;}\n@font-face\n{font-family:Calibri;\npanose-1:2 15 5 2 2 2 4 3 2 4;}\n@font-face\n{font-family:\"\\@宋体\";\npanose-1:2 1 6 0 3 1 1 1 1 1;}\n /* Style Definitions */\n p.MsoNormal, li.MsoNormal, div.MsoNormal\n{margin:0cm;\nmargin-bottom:.0001pt;\ntext-align:justify;\ntext-justify:inter-ideograph;\nfont-size:10.5pt;\nfont-family:\"Calibri\",\"sans-serif\";}\na:link, span.MsoHyperlink\n{mso-style-priority:99;\ncolor:blue;\ntext-decoration:underline;}\na:visited, span.MsoHyperlinkFollowed\n{mso-style-priority:99;\ncolor:purple;\ntext-decoration:underline;}\nspan.EmailStyle17\n{mso-style-type:personal-reply;\nfont-family:\"Calibri\",\"sans-serif\";\ncolor:#1F497D;}\n.MsoChpDefault\n{mso-style-type:export-only;}\n@page Section1\n{size:612.0pt 792.0pt;\nmargin:72.0pt 90.0pt 72.0pt 90.0pt;}\ndiv.Section1\n{page:Section1;}\n-->\n</style>";

String pattern = "(?is)<style>(?:(?!</?style\\b).)*</style>";
String result = test.replaceAll(pattern, "");
System.out.println(result);
PostX 2010-04-16
  • 打赏
  • 举报
回复
测试不通过!
请注意我的里面是有换行的。
david-foreststoe 2010-04-16
  • 打赏
  • 举报
回复
顶楼上
hq1305018 2010-04-16
  • 打赏
  • 举报
回复
请参考下面的程序:

import java.util.ArrayList;

public class Test {

private static ArrayList<String> EXCEPTION_WORD = null;

static {
EXCEPTION_WORD = new ArrayList<String>();
EXCEPTION_WORD.add("\\");
EXCEPTION_WORD.add("*");
EXCEPTION_WORD.add("+");
EXCEPTION_WORD.add(".");
EXCEPTION_WORD.add("?");
EXCEPTION_WORD.add("{");
EXCEPTION_WORD.add("}");
EXCEPTION_WORD.add("(");
EXCEPTION_WORD.add(")");
EXCEPTION_WORD.add("[");
EXCEPTION_WORD.add("]");
EXCEPTION_WORD.add("^");
EXCEPTION_WORD.add("$");
EXCEPTION_WORD.add("-");
EXCEPTION_WORD.add("|");
EXCEPTION_WORD.add("\"");
EXCEPTION_WORD.add("&");
}

/**
* @param args
*/
public static void main(String[] args) {
String text = "<style><!-- /* Font Definitions */ @font-face {font-family:宋体;--></style>aaa";
String pattern = "<style>.+</style>";
System.out.println(text);
text = turnToRegularString(text).replaceAll(pattern,"");
System.out.println(text);
}

public static String turnToRegularString(String old) {

if (old == null || old.trim().length() == 0) {
return old;
}
String regularStr = old.toString();

for (String word : EXCEPTION_WORD) {
if (regularStr.indexOf(word) >= 0) {
regularStr = regularStr.replace(word, "\\" + word);
}
}
return regularStr;
}

}
PostX 2010-04-16
  • 打赏
  • 举报
回复
<style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧