取出html中的符号

从上一步已经得到html页面了,那么就该去掉文件中html的标签

原始文件

我们需要保留文件中的正文而去除html标签:

用sed很easy

sed -e 's/<[^>]*>//g;s/&#160;/ /g;s/&#60;/</g;s/&#62;/>/g;s/&#38;/&/g;s/&#34;/"/g;' test1 |sed -e "s/&nbsp;/ /g;s/&lt;/</g;s/&gt;/>/g;s/\&amp;/\&/g;s/&#39;/'/g" 

解析命令:

1、's/<[^>]*>//g:去掉html标签。注意要加上[^>],如果使用<.*>会出错,如这样的字符串<p><span style="font-size:18px">Given a 2d grid map of <code>'1'</code>s (land) and,使用<.*>会匹配<p><span style="font-size:18px">Given a 2d grid map of <code>'1'</code>

2、转换html中的实体字符

注意&在sed里表示匹配到的项,需要加\转移一下,如:s/\&amp;/\&/g;

显示结果描述实体名称实体编号
 空格&nbsp;&#160;
<小于号&lt;&#60;
>大于号&gt;&#62;
&和号&amp;&#38;
"引号&quot;&#34;
'撇号 &apos; (IE不支持)&#39;
&cent;&#162;
£&pound;&#163;
¥日圆&yen;&#165;
欧元&euro;&#8364;
§小节&sect;&#167;
©版权&copy;&#169;
®注册商标&reg;&#174;
商标&trade;&#8482;
×乘号&times;&#215;
÷除号&divide;&#247;
按照上表转换文档里出现的实体字符

最终效果:

import java.util.*;
import java.io.*;
public class Replace{
        public static void main(String[] args) throws Exception{
                Scanner in = null;
                PrintWriter out = null;
                File inFile = new File("test1");//要去除标签的源文件
                System.out.println(inFile.length());
                File outFile = new File("test2");//保存修改后的文件
                in = new Scanner(inFile);
                out = new PrintWriter(outFile);
                while(in.hasNext()){
                        String str = in.nextLine();
                        out.println(repalce(str));
                }
                System.out.println("Read over!!!");
                in.close();
                out.close();
        }
        public static String repalce(String str){
                //删除标签
                String[] html = {"&#160;","&#60;","&#62;","&#38;","&#34;","&nbsp;","&lt;","&gt;","&amp;","&#39;"};
                String[] re = {" ","<",">","&","\""," ","<",">","&","'"};
                str = str.replaceAll("<[^>]*>","");
                for(int i = 0; i < html.length; i++){
                        str = str.replaceAll(html[i],re[i]);
                }
                return str;
        }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值