82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
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
|
|
{
|
|
// <span\\sclass=\"rantings-num\".*?>(.+?)</span>
|
|
|
|
/// <summary>
|
|
/// 返回正则对象所有结果
|
|
/// </summary>
|
|
/// <param name="html"></param>
|
|
/// <param name="regex"></param>
|
|
/// <returns></returns>
|
|
public static MatchCollection GetMatchCollection(string html, string regex)
|
|
{
|
|
if (string.IsNullOrEmpty(html))
|
|
{
|
|
return null;
|
|
}
|
|
return Regex.Matches(html, regex, RegexOptions.IgnoreCase);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回单个正则分析结果
|
|
/// </summary>
|
|
/// <param name="html"></param>
|
|
/// <param name="regex"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
}
|
|
}
|