word转换html时,会留下很多格式,有些格式并不是我们所需要的,然而这些格式比真正的文章内容还要多,严重影响页面的加载速度,因此就需要找个一个好的解决方案把这些多余的格式个去掉。网上有很多去除word冗余格式的js版的正则表达式,这里只提供java版的正则表达式。
public static String clearWordFormat(String content) {
//把<P></P>转换成</div></div>保留样式
//content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)", "<div$2</div>");
//把<P></P>转换成</div></div>并删除样式
content = content.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");
//删除不需要的标签
content = content.replaceAll("<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>", "");
//删除不需要的属性
content = content.replaceAll("<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>", "<$1$2>");
//删除<STYLE TYPE="text/css"></STYLE>及之间的内容
int styleBegin = content.indexOf("<STYLE");
int styleEnd = content.indexOf("</STYLE>") + 8;
String style = content.substring(styleBegin, styleEnd);
content = content.replace(style, "");
return content;
}
去除不需要的标签
<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>
- match an open tag character <
- and optionally match a close tag sequence </ (because we also want to
remove the closing tags)
- match any of the list of unwanted tags: font,span,xml,del,ins
- a pattern is given to match any of the namespace tags, anything beginning
with o,v,w,x,p, followed by a : followed by another word
- match any attributes as far as the closing tag character >
- the replace string for this regex is "", which will completely remove the
instances of any matching tags.
- note that we are not removing anything between the tags, just the tags
themselves
去除不需要的属性
<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>
- match an open tag character <
- capture any text before the unwanted attribute (This is $1 in the replace
expression)
- match (but don't capture) any of the unwanted attributes: class, lang,
style, size, face, o:p, v:shape etc.
- there should always be an = character after the attribute name
- match the value of the attribute by identifying the delimiters. these can be
single quotes, or double quotes, or no quotes at all.
- for single quotes, the pattern is: ' followed by anything but a ' followed
by a '
- similarly for double quotes.
- for a non-delimited attribute value, i specify the pattern as anything
except the closing tag character >
- lastly, capture whatever comes after the unwanted attribute in ([^>]*)
- the replacement string <$1$2> reconstructs the tag without the
unwanted attribute found in the middle.
- note: this only removes one occurence of an unwanted attribute, this is why
i run the same regex twice. For example, take the html fragment: <p
class="MSO Normal" style="Margin-TOP:3em">
the regex will only remove
one of these attributes. Running the regex twice will remove the second one. I
can't think of any reasonable cases where it would need to be run more than
that.
分享到:
相关推荐
有时候我们需要将js的注释去掉,减少代码中的冗余,有时候注释太多导致页面体积大。 注释图示如下: 一.匹配多行注释正则表达式: /(?:^|\n|\r)\s*\/\*[\s\S]*?\*\/\s*(?:\r|\n|$)/g 二.单行注释正则表达式: /(?:^|...
说明:在1.0的版本上优化了读写方式,解决了数据可能出现的冗余问题 (java程序员专用,没安装jdk1.6的也不要下载)。能对输入的字符串进行验证,还能收藏你常用的正则规则。使用环境。jkd6.0 ,linux/windows操作系统...
用Teleport pro对某站点整站下载之后,用Dreamweaver打开,发现代码里有大量的冗余代码
自己用python做的编译原理作业,代码有点冗余,没有完善,顺利通过老师检查 编译原理(由字母表通过运用调度场算法到逆波兰表达式到NFA到DFA到最小化DFA),python,graphiviz实现可视化
29-正则表达式-re属性-分组.flv 30-爬虫.flv7 31-深拷贝-浅拷贝.flv 33-文件-2-文件对象的方法.flv 34-文件-3-OS模块.flv 35-目录遍历-杀毒软件.flv 36-异常处理.flv 38-面向对象-1-类和对象.flv 37-MySQLdb.flv
软著申请时需要上传的文件: ...3、代码整理小技巧:针对代码冗余的整理,相关正则表达式的替换,使得申请代码的整理方便又快速。 总结:模板主要对软著申请的细节做了约束,使用时只需替换相关内容即可。
CRC冗余校验,使用java实现,共同学习!!
java实现的类everything源码,当前未支持文件添加,删除操作的更新,有一些日志方便开发使用,当然代码有一些冗余,当前支持,多字符串搜索,支持正则表达式搜索,用于学习,来自于: ...
可以在 DreamWeaver中使用正则表达式批量清除这种代码. 具体写法如下: 1、匹配tppabs标签: \btppabs="h[^"]*" 替换为 (空) 2、匹配javascript代码: href="javascript:if\(confirm\('htt[^"]*" 替换为 href=...
不仅如此,作者还介绍了散列和散列表、优先队列、状态机和正则表达式以及诸如哈夫曼和LZ77等数据压缩技术。 随附光盘中有作者所开发的一个相当成功的自由软件库EZDSL,另外还有可运行于各版本Delphi上和Kylix上的源...
不仅如此,作者还介绍了散列和散列表、优先队列、状态机和正则表达式以及诸如哈夫曼和LZ77等数据压缩技术。 随附光盘中有作者所开发的一个相当成功的自由软件库EZDSL,另外还有可运行于各版本Delphi上和Kylix上的源...
可配置:线程数、线程等待时间,连接超时时间,可爬取文件类型和优先级、下载目录等。 状态栏显示统计信息:排入队列URL数,已下载文件数,已下载...较好的性能:基于正则表达式的页面解析、适度加锁、维持HTTP连接等。
该系统基本包含一般处理SQL的命令,...在校验方面,多是使用正则表达式.在一次性频繁处理数据库的更新操作的时候,偶尔使用多线程,自定义传递参数的类,来给多线程传递参数.该系统适合初学者,希望能给大家帮助...谢谢...
去除tppabs冗余代码,轻松搞定
主要特性有: • 可配置:线程数、线程等待时间,连接超时时间,可爬取文件类型和优先级、下载目录等。 • 状态栏显示统计信息:排入队列URL...• 较好的性能:基于正则表达式的页面解析、适度加锁、维持HTTP连接等。
这是一款用 C# 编写的网络爬虫 主要特性有: 可配置:线程数、线程等待时间,连接超时时间,可爬取文件类型和优先级、下载目录等。...较好的性能:基于正则表达式的页面解析、适度加锁、维持HTTP连接等。
在校验方面,多是使用正则表达式.在一次性频繁处理数据库的更新操作的时候,偶尔使用多线程,自定义传递参数的类,来给多线程传递参数.该系统适合初学者,希望能给大家帮助...谢谢... 菜单功能: 基本信息管理 人事管理 ...
正则表达式就是那种,写的时候看文档,写完之后能用,过段时间看不懂,下一次写的时候又要看文档...这里总结复习一下基础语法及常用例子,也方便快速查阅 SQL相关 由于以前爬虫获取的豆瓣电影资料库数据库结构考虑...
所见即所得的打印方式,支持多种颜色方式打印源代码,能够同时编辑多个文件,支持同步编辑多重窗口同步,除了具备一般的搜索替换功能外还支持正则表达式搜索。支持文件拖曳功语言。支持多国语言。无论是日常的应用...