/*
* 常用正则表达式
* 肖俊
* 2013-5-2
*
*/
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AllBase
{
///
/// 基础正则表达式类
///
public static class BaseRegex
{
///
/// 返回正则对象所有结果
///
///
///
///
public static MatchCollection GetMatchRegex(string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return null;
}
return Regex.Matches(HtmlString, RegexString, RegexOptions.IgnoreCase);
}
///
/// 返回正则对象所有结果
///
///
///
///
public static MatchCollection GetMatchRegex(ref string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return null;
}
return Regex.Matches(HtmlString, RegexString, RegexOptions.IgnoreCase);
}
///
/// 返回单个正则分析结果
///
///
///
///
public static string GetMatchRegexString(ref string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return null;
}
MatchCollection mc = Regex.Matches(HtmlString, RegexString, RegexOptions.IgnoreCase);
if (mc.Count > 0)
{
return mc[0].Groups[1].Value.ToString();
}
return null;
}
///
/// 返回单个正则分析结果
///
///
///
///
public static string GetMatchMaxRegexString(ref string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return null;
}
MatchCollection mc = Regex.Matches(HtmlString, RegexString, RegexOptions.IgnoreCase);
if (mc.Count > 0)
{
return mc[mc.Count - 1].Groups[1].Value.ToString();
}
return null;
}
///
/// 返回匹配的数值
///
///
///
///
public static int GetMatchInt(ref string HtmlString, string RegexString)
{
string id = GetMatchRegexString(ref HtmlString, RegexString);
int i;
int.TryParse(id, out i);
return i;
}
///
/// 返回单个正则分析结果
///
///
///
///
public static string GetMatchRegexString(string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return null;
}
MatchCollection mc = Regex.Matches(HtmlString, RegexString, RegexOptions.IgnoreCase);
if (mc.Count > 0)
{
return mc[0].Groups[1].ToString();
}
return null;
}
///
/// 是否存在匹配
///
///
///
///
public static bool MatchRegex(string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return false;
}
return Regex.IsMatch(HtmlString, RegexString, RegexOptions.IgnoreCase);
}
///
/// 根据正则删除表达式所匹配的内容
///
///
///
///
public static string Clear(string HtmlString, string RegexString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空,正则:" + RegexString);
return null;
}
return Regex.Replace(HtmlString, RegexString, "", RegexOptions.IgnoreCase);
}
///
/// 清除HTML代码
///
///
/// 返回文字内容
public static string ClearHtml(string HtmlString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空");
return null;
}
return Regex.Replace(Regex.Replace(HtmlString,
@"(
|||)", ""), "(<[^>]*?>)", "");
}
///
/// 清除HTML代码A标签
///
///
/// 返回文字内容
public static string ClearA(string HtmlString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空");
return null;
}
HtmlString = Regex.Replace(HtmlString, "(?a[^>]*>)", "", RegexOptions.IgnoreCase);
return HtmlString.Trim();
}
///
/// 去除空格和特殊空白
///
///
///
public static string Trim(string text)
{
if (string.IsNullOrEmpty(text))
return "";
return text.Replace("\t", " ").Replace("\r\n", " ").Replace("\r", " ").Replace("\n", " ").Trim();
}
///
/// 清除HTML代码A标签
///
///
/// 返回文字内容
public static string ClearTitle(string HtmlString)
{
if (HtmlString == null)
{
LogSave.Add("匹配正则发生错误:原始字符串为空");
return null;
}
HtmlString = Regex.Replace(HtmlString, "(title=\"[^\"]*\")", "");
return HtmlString.Trim();
}
///
/// 获取值
///
///
///
///
public static string GetValue(string html, string reg)
{
return GetMatchRegexString(html, "<" + reg + ">([^<]+)" + reg + ">");
}
///
/// 清理多余的HTML元素
///
///
///
public static string ClearNone(string html)
{
if (string.IsNullOrEmpty(html))
return null;
if (html.Contains("\0") && html.IndexOf("\0") > 0)
html = html.Substring(0, html.IndexOf("\0"));
string h = Clear(html, @"(<\!doctype[^>]*?>)");
h = Clear(h, @"(]*?>)");
h = Clear(h, @"()");
h = Clear(h, @"(