java中利用正则,过滤网页标签.......

在开发中有时候会遇到在一大串字符串中替换或者去除某个特定的字符串,一下例子是过滤html页面字符串的实例,说明正则在其中的作用:

package com.project.admin.common.util;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * html标签
 * @author xwp
 *
 */
public class HtmlUtil {
//去除某种视频引用方法,然后替换成新的
private static final String pc_embed_first="http://player.youku.com/player.php/sid/";
private static final String pc_embed_end="==/isShowRelatedVideo";
private static final String embed_tag="<embed[^>]*?[\\s\\S]*? \\/>";
private static final String smallVideo="<div id='youkuplayer' style='width:6.4rem;height:3.2rem'></div><script type='text/javascript' src='http://player.youku.com/jsapi'>                  </script><script type='text/javascript'>player = new YKU.Player('youkuplayer',{styleid: '0',client_id: 'ec5fe2a0dce21ad3',vid: '###videoAddress###',newPlayer: true});               </script>";


    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式  
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式  
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式  
    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符  
     
    //去除行样式
    private static final String remove_style ="style=\"[^>]*?;\""; // 定义style的正则表达式
    private static final String remove_width ="width=\"[^>]*?\""; // 定义style的正则表达式
    private static final String remove_height ="height=\"[^>]*?\""; // 定义style的正则表达式
      
    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delHTMLTag(String htmlStr) {  
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);  
        Matcher m_script = p_script.matcher(htmlStr);  
        htmlStr = m_script.replaceAll(""); // 过滤script标签  
  
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);  
        Matcher m_style = p_style.matcher(htmlStr);  
        htmlStr = m_style.replaceAll(""); // 过滤style标签  
  
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
        Matcher m_html = p_html.matcher(htmlStr);  
        htmlStr = m_html.replaceAll(""); // 过滤html标签  
  
        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);  
        Matcher m_space = p_space.matcher(htmlStr);  
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签  
        return htmlStr.trim(); // 返回文本字符串  
    }  
    //删除Html标签  
    public static String getTextFromHtml(String htmlStr){  
        htmlStr = delHTMLTag(htmlStr);  
        htmlStr = htmlStr.replaceAll("&nbsp;", "");  
        return htmlStr;  
    }  
    //去除style行样式
    public static String removeStyleHtml(String htmlStr){ 
    Pattern p_style = Pattern.compile(remove_style, Pattern.CASE_INSENSITIVE);  
        Matcher m_style = p_style.matcher(htmlStr);  
        htmlStr = m_style.replaceAll(""); // 过滤style标签  
        
        Pattern p_width = Pattern.compile(remove_width, Pattern.CASE_INSENSITIVE);  
        Matcher m_width = p_width.matcher(htmlStr);  
        htmlStr = m_width.replaceAll(""); // 过滤style标签  
        
        Pattern p_height = Pattern.compile(remove_height, Pattern.CASE_INSENSITIVE);  
        Matcher m_height = p_height.matcher(htmlStr);  
        htmlStr = m_height.replaceAll(""); // 过滤style标签  
        
        //System.out.println(htmlStr); 
        return htmlStr;  
    }

    public static List<String> getVideoId(String htmlStr){                            //获取vid
    List<String> results = new ArrayList<String>();
Pattern p=Pattern.compile(pc_embed_first+"(.*?)"+pc_embed_end);
   Matcher m=p.matcher(htmlStr);
   while(!m.hitEnd() && m.find()){
       results.add(m.group(1));
   }
    return results;
    }
    
    public static List<String> getEmbedTag(String htmlStr){                          //获取embed标签
    List<String> results = new ArrayList<String>();
    Pattern pp = Pattern.compile(embed_tag);  
        Matcher mp = pp.matcher(htmlStr);  
        while(!mp.hitEnd() && mp.find()){
       results.add(mp.group(0));
   }
        return results;  
    }
    
    //处理带有视频的新闻内容,,供微官网用
    public static String doSmallVideo(String htmlStr){ 
    if(htmlStr.contains(pc_embed_first)){
    List<String> videoIds=getVideoId(htmlStr);
        List<String> embedTags=getEmbedTag(htmlStr);
       
        if(!videoIds.isEmpty() && !embedTags.isEmpty() && videoIds.size()==embedTags.size()){
        for (int i=0;i<embedTags.size();i++){
            String tempTag=smallVideo;
            tempTag=tempTag.replace("###videoAddress###",videoIds.get(i));
            htmlStr=htmlStr.replaceAll(embedTags.get(i),tempTag);
        }
        }
    }
    return htmlStr;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,账号密码正则特殊符号过滤可以通过使用正则表达式来实现。正则表达式是一种特殊的文本字符串,可以用于匹配、查找、替换等操作。在此过程,可以使用特定的符号来匹配特定的字符,如字母、数字、空格、特殊符号等。在这个问题,我们可以使用正则表达式来匹配账号密码的特殊符号,从而实现过滤的功能。 使用正则表达式需要先创建一个正则表达式对象,并指定需要匹配的字符串。然后,可以使用不同的符号来匹配特定的字符,如\d表示匹配数字字符,\w表示匹配字母、数字和下划线,\s表示匹配空格等。 在本问题需要过滤的特殊符号可能包括:$、^、*、+、?、=、{、}、[、]、|、\、/等。这些符号在正则表达式都是有特殊意义的,需要使用转义符\来进行转义,以匹配实际的字符。 例如,可以使用如下的正则表达式过滤账号密码的特殊符号:(具体的正则表达式可能因应用场景而有所不同) ``` String regex = "[^a-zA-Z0-9\\s]"; // 过滤除字母、数字、空格之外的所有字符 String account = "example@$123"; // 待过滤的账号 String password = "p^w=d\\/r"; // 待过滤的密码 String filteredAccount = account.replaceAll(regex, ""); // 过滤账号的特殊符号 String filteredPassword = password.replaceAll(regex, ""); // 过滤密码的特殊符号 ``` 这里的^表示除了[]的任意一个字符之外的任意字符;$表示字符串结尾;/表示转义字符。在实现时,可以按需修改正则表达式,以匹配特定的字符集合,同时保留必要的特殊符号。匹配完成后,可以使用String的replaceAll()方法来替换掉所有匹配的字符,从而完成账号密码正则特殊符号过滤的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值