using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace NaiBa.Util.Helper { public class RegexHelper { // (.+?) /// /// 返回正则对象所有结果 /// /// /// /// public static MatchCollection GetMatchCollection(string html, string regex) { if (string.IsNullOrEmpty(html)) { return null; } return Regex.Matches(html, regex, RegexOptions.IgnoreCase); } /// /// 返回单个正则分析结果 /// /// /// /// public static string GetMatch(string html, string regex, int index = 0) { if (string.IsNullOrEmpty(html)) { return string.Empty; } MatchCollection mc = Regex.Matches(html, regex, RegexOptions.IgnoreCase); if (mc.Count > 0) { return mc[index].Groups[1].Value.ToString(); } return string.Empty; } public static string GetMatchWithSingleLine(string html, string regex) { if (string.IsNullOrEmpty(html)) { return string.Empty; } MatchCollection mc = Regex.Matches(html, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline); if (mc.Count > 0) { return mc[0].Groups[1].ToString(); } return string.Empty; } public static bool IsNumber(string str) { Regex regex = new Regex(@"^[0-9\.]+$"); return regex.IsMatch(str); } public static bool IsAlphabet(string str) { Regex regex = new Regex(@"^[A-Za-z]+$"); return regex.IsMatch(str); } public static bool IsAlphabetOrNumber(string str) { Regex regex = new Regex(@"^[A-Za-z0-9]+$"); return regex.IsMatch(str); } } }