常用正则表达式

1、从Xml中提取Encoding值的
<\?xml\s+[^>]*Encoding=('|")?(?<Encoding>[^>\s'"]*)('|")?[^>]*> 

2、过滤所有HTML标签取纯文本

Code
/**//// <summary>
        
/// 过滤所有HTML标签取纯文本
        
/// </summary>
        
/// <param name="html"></param>
        
/// <returns></returns>

        public static string FilterHTMLTags(string html)
        
{
            html 
= html.Replace("\n\r"" ").Replace("\r\n"" ").Replace("\n"" ").Replace("\r"" ").Replace("\t""    ");

            Regex regex1 
= new Regex(@"<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>", RegexOptions.IgnoreCase);
            Regex regex2 
= new Regex(@"<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>", RegexOptions.IgnoreCase);
            Regex regex3 
= new Regex(@"<[^>]+>", RegexOptions.IgnoreCase);
            
//Regex regex7 = new Regex(@"<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>", RegexOptions.IgnoreCase);

            html 
= regex1.Replace(html, string.Empty);                      //过滤<script></script>标记           
            html = regex2.Replace(html, string.Empty);                      //过滤<style> 里面的内容 
            html = regex3.Replace(html, string.Empty);                      //过滤<> 里面的内容 
            return html;
        }




3、过滤不安全的HTML脚本

Code
/**//// <summary>
        
/// 正则表达式过滤不安全的HTML脚本 
        
/// </summary>
        
/// <param name="html">输入的Html</param>
        
/// <returns>过滤净化后的Html</returns>

        public static string Wipescript(string html)
        
{
            Regex regex1 
= new Regex(@"<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>", RegexOptions.IgnoreCase);

            Regex regex2 
= new Regex(@"</?form[^>]*>", RegexOptions.IgnoreCase);
            Regex regex3 
= new Regex(@"<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>", RegexOptions.IgnoreCase);
            html 
= regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, "");
            html 
= regex3.Replace(html, "");
            
return html;
        }


4、取所有src链接
(\ssrc=)(?<url>[^>\s]*)  

5、取所有图片链接地址

Code
 /**//// <summary>
        
/// 获取HTML中所有图片的地址
        
/// </summary>
        
/// <param name="html">原HTML</param>
        
/// <returns>HashTable 所有图片链接</returns>

        public static Hashtable GetImg(string html)
        
{
            MatchCollection mm 
= Regex.Matches(html, @"<img\s+[^>]*src=\s*('|"")*(?<url>[^>\s'""]*)('|"")*[^>]*>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);           

            
int i = 0;
            
bool blnRepeat;
            Hashtable htb 
= new Hashtable();

            
if (mm != null)
            
{
                
foreach (Match ss in mm)
                
{
                    
if (i == 0)
                    
{
                        i
++;
                        htb.Add(
0, ss.Groups["url"].Value);
                    }

                    
else
                    
{
                        blnRepeat 
= false;
                        
foreach (DictionaryEntry de in htb)
                        
{
                            
if (ss.Groups["url"].Value.ToLower() == de.Value.ToString().ToLower())
                            
{
                                blnRepeat 
= true;
                                
break;
                            }

                        }

                        
if (blnRepeat == false)
                        
{
                            htb.Add(i, ss.Groups[
"url"].Value);
                            i
++;
                        }

                    }

                }

            }


            
return htb;
        }

 6、高亮显示文本内容

Code
/**//// <summary>
        
/// 将文本标记为高亮
        
/// </summary>
        
/// <param name="textToShow">要显示的文本</param>
        
/// <param name="keysString">高亮匹配的关键字串(逗号分隔)</param>
        
/// <returns>返回高亮标记后的文本</returns>

        public static string MarkHightLightText(string textToShow, string keysString)
        
{
            
string[] strAkeys = keysString.Split(new char[] ',''' }, StringSplitOptions.RemoveEmptyEntries);
            
foreach (string keyString in strAkeys)
            
{
                System.Text.RegularExpressions.MatchCollection m 
= Regex.Matches(textToShow, keyString, RegexOptions.IgnoreCase);//忽略大小写搜索字符串中的关键字
                for (int j = 0; j < m.Count; j++)//循环在匹配的子串前后插东东
                {

                    
//j×31为插入html标签使pain字符串增加的长度:
                    textToShow = textToShow.Insert((m[j].Index + keyString.Length + j * 31), "</span>");//关键字后插入html标签
                    textToShow = textToShow.Insert((m[j].Index + j * 31), "<span class=\"Highlight\">");//关键字前插入html标签
                }

            }

            
return textToShow;     
        }


         

7、在页面头部找feed链接

<link[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>

例:

Code
/**//// <summary>
        
/// 在Html中找Feed地址
        
/// </summary>
        
/// <param name="strHtml">可能包含Feed地址的Html</param>
        
/// <returns></returns>

        public static string GetFeedAddressInHtml(string strHtml)
        
{
            
string strFeedAddress = string.Empty;
            
try
            
{
                
if (strHtml != string.Empty)
                
{
                    
int intHeaderEndIndex = strHtml.ToLower().IndexOf("</head>");
                    
if (intHeaderEndIndex > -1)
                    
{
                        
// 截取头部代码
                        string strHeader = strHtml.Substring(0, strHtml.ToLower().IndexOf("</head>"));

                        
string strRegex = @"<link[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>";
                        MatchCollection results 
= Regex.Matches(strHeader, strRegex);

                        
foreach (Match res in results)
                        
{
                            
string strFeed = res.Groups["href"].Value.Replace("\"", string.Empty).Trim();
                            string strExtend = strFeed.Substring(strFeed.LastIndexOf('.'+ 1, strFeed.Length - strFeed.LastIndexOf('.'- 1);

                            
if (_blackExtendNameString.IndexOf("," + strExtend.Trim().ToLower() + ",">= 0)
                            
{
                                
continue;
                            }

                            
else
                            
{
                                strFeedAddress 
= strFeed;
                                
break;
                            }

                        }

                    }

                }

            }

            
catch (Exception ex)
            
{
                Log.Write(
"在Html中查找Feed地址出错");
                Log.Write(ex);                
            }
            
            
            
return strFeedAddress;
        }


         

8、分离Url参数和锚点

Code
Regex rex = new Regex("#.+");
                MatchCollection mct 
= rex.Matches(Request.QueryString["idx"]);
                
string strAnchor = mct == null ? string.Empty : mct.Count == 0 ? string.Empty : mct[0].Value.Trim();
                
string strIdx = rex.Replace(Request.QueryString["idx"], ""); 


9、过滤unicode编码

     \&\#\d*;

 

附:C#中调用正则表达式匹配的代码示例

Code
MatchCollection results = Regex.Matches(strHtml, @"<\?xml\s+[^>]*Encoding=('|"")?(?<Encoding>[^>\s'""]*)('|"")?[^>]*>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

 

 
foreach (Match res in results)
 
{
     strEncoding 
= res.Groups["Encoding"].Value;
 }

转载于:https://www.cnblogs.com/litsword/articles/1182053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中常用正则表达式有很多种,以下是一些常见的用法: 1. 匹配手机号码:^1\[3456789\]\d{9}$ 这个正则表达式可以用来匹配以1开头的11位数字,其中第二位是3、4、5、6、7、8、9中的一个。 2. 匹配邮箱地址:^\w+(\[-+.\]\w+)*@\w+(\[-.\]\w+)*\.\w+(\[-.\]\w+)*$ 这个正则表达式可以用来匹配常见的邮箱地址格式,包括用户名部分、@符号、域名部分和后缀部分。 3. 匹配身份证号码:(^\d{15}$)|(^\d{17}(\[0-9\]|X)$) 这个正则表达式可以用来匹配15位或18位的身份证号码,其中最后一位可以是数字或大写字母X。 4. 匹配URL地址:^(https?|ftp)://\[^\s/$.?#\].\[^\s\]*$ 这个正则表达式可以用来匹配常见的URL地址格式,包括协议部分(http、https、ftp)、域名部分和路径部分。 这些只是一些常见的正则表达式用法,实际上还有很多其他的用法和规则可以根据具体需求进行定制。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [Java 之正则表达式语法及常用正则表达式汇总](https://blog.csdn.net/La_Grace/article/details/129786033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Java 正则表达式(深度长文)](https://blog.csdn.net/senxu_/article/details/126109760)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值