public class RegexHelper
{
/// <summary>
/// 验证输入字符串是否与模式字符串匹配,匹配返回true
/// </summary>
/// <param name="input">输入字符串</param>
/// <param name="pattern">模式字符串</param>
public static bool IsMatch(string input, string pattern)
{
return IsMatch(input, pattern, RegexOptions.IgnoreCase);
}
/// <summary>
/// 验证输入字符串是否与模式字符串匹配,匹配返回true
/// </summary>
/// <param name="input">输入的字符串</param>
/// <param name="pattern">模式字符串</param>
/// <param name="options">筛选条件</param>
public static bool IsMatch(string input, string pattern, RegexOptions options)
{
return Regex.IsMatch(input, pattern, options);
}
/// <summary>
/// 搜索匹配的值
/// </summary>
/// <param name="str"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public MatchCollection Matches(string str, string pattern)
{
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase); // 搜索匹配的字符串
var list = reg.Matches(str);
return list;
}
/// <summary>
/// 正在表达式 拆分字符串
/// </summary>
/// <param name="str"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public string[] Split(string str, string pattern)
{
var items = Regex.Split(str, pattern);
return items;
}
/// <summary>
/// 正则替换字符串
/// </summary>
/// <param name="input">要搜索匹配项的字符串</param>
/// <param name="pattern">要匹配的正则表达式模式</param>
/// <param name="replacement">替换字符串</param>
/// <returns></returns>
public string Replace(string input, string pattern, string replacement)
{
var str = Regex.Replace(input, pattern, replacement);
return str;
}
}