#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
/// <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