using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace System.Common { /// /// 正则表达式公共类 /// public class RegexHelper { /// /// 检查是否匹配 /// /// /// /// /// public static bool IsMatch(string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase) { return Regex.IsMatch(str, pattern, options); } /// /// 获取匹配对象 /// /// /// /// /// public static Match GetMatch(string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase) { return Regex.Match(str, pattern, options); } /// /// 从字符串中找到匹配的字符 /// /// 原始字符 /// 正则 /// 分隔符 /// public static List FindMatchs(string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase) { var list = new List(); var matchs = Regex.Matches(str, pattern, options); foreach (Match matcher in matchs) { if (matcher == null || !matcher.Success) { continue; } list.Add(matcher); } return list; } /// /// 用正则替换 /// /// /// /// /// public static string Replace(string str, string replacement, string pattern, RegexOptions options = RegexOptions.IgnoreCase) { return Regex.Replace(str, pattern, replacement, options); } } }