正则表达式的应用(Seaskyer)

#region  正则表达式的使用

        
///   <summary>
        
///  判断输入的字符串是否完全匹配正则
        
///   </summary>
        
///   <param name="RegexExpression"> 正则表达式 </param>
        
///   <param name="str"> 待判断的字符串 </param>
        
///   <returns></returns>
         public   static   bool  IsValiable( string  RegexExpression,  string  str)
        {
            
bool  blResult  =   false ;

            Regex rep 
=   new  Regex(RegexExpression, RegexOptions.IgnoreCase);

            
// blResult = rep.IsMatch(str);
            Match mc  =  rep.Match(str);

            
if (  mc.Success )
            {
                
if ( mc.Value  ==  str ) blResult  =   true ;
            }


            
return  blResult;
        }

        
///   <summary>
        
///  转换代码中的URL路径为绝对URL路径
        
///   </summary>
        
///   <param name="sourceString"> 源代码 </param>
        
///   <param name="replaceURL"> 替换要添加的URL </param>
        
///   <returns> string </returns>
         public   static   string  convertURL( string  sourceString, string  replaceURL)
        {
            Regex rep 
=   new  Regex( "  (src|href|background|value)=('|"|)([^('|"|)http://].*?)('|"| |>) " );
            sourceString 
=  rep.Replace(sourceString, "  $1=$2 "   +  replaceURL  +   " $3$4 " );

            
return  sourceString;
        }

        
///   <summary>
        
///  获取代码中所有图片的以HTTP开头的URL地址
        
///   </summary>
        
///   <param name="sourceString"> 代码内容 </param>
        
///   <returns> ArrayList </returns>
         public   static  ArrayList GetImgFileUrl( string  sourceString)
        {
            ArrayList imgArray 
=   new  ArrayList();

            Regex r 
=   new  Regex( " <IMG(.*?)src=('|"|)(http://.*?)('|"| |>) " , RegexOptions.IgnoreCase | RegexOptions.Compiled);
            
            MatchCollection mc 
=   r.Matches(sourceString);
            
for  ( int  i  =   0 ; i  <  mc.Count; i ++
            {
                
if ! imgArray.Contains(mc[i].Result( " $3 " )) )
                {
                    imgArray.Add(mc[i].Result(
" $3 " ));
                }
            }

            
return  imgArray;
        }

        
///   <summary>
        
///  获取代码中所有文件的以HTTP开头的URL地址
        
///   </summary>
        
///   <param name="sourceString"> 代码内容 </param>
        
///   <returns> ArrayList </returns>
         public   static  Hashtable getFileUrlPath( string  sourceString)
        {
            Hashtable url 
=   new  Hashtable();

            Regex r 
=   new  Regex( "  (src|href|background|value)=('|"|)(http://.*?)('|"| |>) " ,
                RegexOptions.IgnoreCase
| RegexOptions.Compiled);

            MatchCollection mc 
=   r.Matches(sourceString);
            
for  ( int  i  =   0 ; i  <  mc.Count; i ++
            {
                
if ! url.ContainsValue(mc[i].Result( " $3 " )) )
                {
                    url.Add(i, mc[i].Result(
" $3 " ));
                }
            }

            
return  url;
        }

        
///   <summary>
        
///  获取一条SQL语句中的所参数
        
///   </summary>
        
///   <param name="sql"> SQL语句 </param>
        
///   <returns></returns>
         public   static  ArrayList SqlParame( string  sql)
        {
            ArrayList list 
=   new  ArrayList();
            Regex r 
=   new  Regex( @" @(?<x>[0-9a-zA-Z]*) " , RegexOptions.IgnoreCase | RegexOptions.Compiled);
            MatchCollection mc 
=   r.Matches(sql);
            
for ( int  i  =   0 ; i  <  mc.Count; i ++ )
            {
                list.Add(mc[i].Result(
" $1 " )); 
            }

            
return  list;
        }

        
///   <summary>
        
///  获取一条SQL语句中的所参数
        
///   </summary>
        
///   <param name="sql"> SQL语句 </param>
        
///   <returns></returns>
         public   static  ArrayList OracleParame( string  sql)
        {
            ArrayList list 
=   new  ArrayList();
            Regex r 
=   new  Regex( @" :(?<x>[0-9a-zA-Z]*) " , RegexOptions.IgnoreCase | RegexOptions.Compiled);
            MatchCollection mc 
=   r.Matches(sql);
            
for ( int  i  =   0 ; i  <  mc.Count; i ++ )
            {
                list.Add(mc[i].Result(
" $1 " )); 
            }

            
return  list;
        }

        
///   <summary>
        
///  将HTML代码转化成纯文本
        
///   </summary>
        
///   <param name="sourceHTML"> HTML代码 </param>
        
///   <returns></returns>
         public   static   string  convertText( string  sourceHTML)
        {
//             string strResult = "";
//             Regex r = new Regex(">(.*?)<", RegexOptions.IgnoreCase|RegexOptions.Compiled);
//             MatchCollection mc =  r.Matches(sourceHTML);
//
//             if( mc.Count == 0 )
//             {
//                 strResult = sourceHTML;
//             }
//             else
//             {
//                 for(int i = 0; i < mc.Count; i++)
//                 {
//                     strResult += mc[i].Result("$1"); 
//                 }
//             }

            
string  strResult  =   "" ;
            Regex r 
=   new  Regex( " <(.*?)> " , RegexOptions.IgnoreCase | RegexOptions.Compiled);
            MatchCollection mc 
=   r.Matches(sourceHTML);

            
if ( mc.Count  ==   0  )
            {
                strResult 
=  sourceHTML;
            }
            
else
            {
                strResult 
=  sourceHTML;

                
for ( int  i  =   0 ; i  <  mc.Count; i ++ )
                {
                    strResult 
=  strResult.Replace(mc[i].ToString(),  "" );
                }
            }

            
return  strResult;
        }
        
#endregion
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值