using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace Vinno.vCloud.Disk { public class RegexHelper { private RegexHelper(){} /// /// 正则表达式替换 /// /// /// /// /// public static string Replace(string input,string pattern,string replacement) { StringBuilder stringBuilder = new StringBuilder(input); var matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase); if (matches.Count > 0) { foreach (Match match in matches) { if (match.Success && !string.IsNullOrWhiteSpace(match.Value)) { if (match.Groups.Count > 1) { stringBuilder.Replace(match.Groups[1].Value, replacement); } else { stringBuilder.Replace(match.Value, replacement); } } } } return stringBuilder.ToString(); } public static string MatchSub(string input, string pattern, RegexOptions regexOptions = RegexOptions.IgnoreCase) { var match = Regex.Match(input, pattern, regexOptions); if (match.Success && match.Groups.Count>1) { return match.Groups[1].Value; } return string.Empty; } public static bool IsMatch(string input, string pattern, RegexOptions regexOptions = RegexOptions.IgnoreCase) { return Regex.IsMatch(input, pattern, regexOptions); } } }