• Java 使用正则表达式


    用正则表达式来处理掉内容中的特定字符,下面的代码为,去掉P标签中的属性width 设置。将P标签处理后在拼接成字符串

    /**
         * 给 P 标签去掉width 样式设置
         * @param content
         * @return
         */
        public static String formatCodeP(String content)
        {
            String regex = "<p [^>]*?>";
            Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);   
            Matcher m = pattern.matcher(content);   
            StringBuilder builder = new StringBuilder();
            int sindex = 0;//主要用于标示查找起始位置
            int eindex = 0;//主要用于标示查找起始位置
            while(m.find()){
                String data = m.group();
                eindex = m.start();
                builder.append(content.substring(sindex,eindex));
                data = data.replaceAll("width[^;]*?;", "");
                builder.append(data);
                //
                sindex = m.end();
            }
            builder.append(content.substring(sindex));
            return builder.toString();
        }
        
        public static void main(String[] args)
        {
            String content = "<p style="table-layout: fixed; font-size: 14px; word-break: break-all; line-height: 28px; letter-spacing: 1px;  1175px; clear: both; word-wrap: break-word; margin: 10px auto; color: rgb(51, 51, 51); white-space: normal; font-family: verdana, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近在制作一个模版的时候用到的一个jquery插件,当网站导航滚动到当前可见页面顶部时,固定在顶部并随窗口滚动,有很多的网站前台模版有有类似的效果。</p>";
            String bString = formatCodeP(content);
            System.out.println(bString);
        }
    

      

  • 相关阅读:
    ArcGIS Engine 常用方法(转)
    正则表达式 C#System.Text.RegularExpressions.Regex
    ae中栅格数据转为矢量数据 (转)
    ArcEngine 渲染的使用 (转)
    C#字符串分割成数组,中间多空格
    <C++ GUI Programming with Qt4 ,Second Edition> 学习笔记
    perl module and its package
    static_cast reinterpret_cast
    阅读<inside the c++ object modle > 有感
    C++ virtual table
  • 原文地址:https://www.cnblogs.com/panie2015/p/5613156.html
Copyright © 2020-2023  润新知