|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
namespace demo.ClassHelper.RegexMatch
|
|
|
{
|
|
|
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="strInput"></param>
|
|
|
/// <returns></returns>
|
|
|
public static bool IsDigitalStr(string strInput)
|
|
|
{
|
|
|
if (strInput != null && strInput.Length > 0)
|
|
|
{
|
|
|
Regex rx = new Regex(@"\.", RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
MatchCollection matches = rx.Matches(strInput);
|
|
|
|
|
|
if (matches.Count > 1)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (strInput.IndexOf('.') != 0 && strInput.IndexOf('.') != strInput.Length - 1)
|
|
|
{
|
|
|
int count = 0;
|
|
|
if (matches.Count == 1)
|
|
|
strInput = strInput.Remove(strInput.IndexOf('.'), 1);
|
|
|
count = strInput.Length;
|
|
|
if (strInput.Contains('-'))
|
|
|
count = strInput.Length - 1;
|
|
|
|
|
|
Regex rxIsDigit = new Regex(@"[\-]*\d{" + count.ToString() + "}", RegexOptions.IgnoreCase
|
|
|
| RegexOptions.Multiline);
|
|
|
|
|
|
if (rxIsDigit.IsMatch(strInput))
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="strInput">输入字符</param>
|
|
|
/// <param name="strRegex">匹配表达式</param>
|
|
|
/// <returns>返回字符</returns>
|
|
|
public static string StrRegexText(string strInput, string strRegex)
|
|
|
{
|
|
|
Regex rx = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
Match match = rx.Match(strInput);
|
|
|
|
|
|
return match.Groups[0].Value;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="strInput"></param>
|
|
|
/// <param name="strRegex">匹配表达式</param>
|
|
|
/// <returns>返回整型</returns>
|
|
|
public static int intRegexText(string strInput, string strRegex)
|
|
|
{
|
|
|
Regex rx = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
Match match;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
match = rx.Match(strInput);
|
|
|
|
|
|
if (match.Groups[0].Value != null && match.Groups[0].Value.Length > 0)
|
|
|
{
|
|
|
return int.Parse(match.Groups[0].Value);
|
|
|
}
|
|
|
}
|
|
|
catch { }
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="strInput"></param>
|
|
|
/// <param name="strRegex">匹配表达式</param>
|
|
|
/// <returns></returns>
|
|
|
public static double douRegexText(string strInput, string strRegex)
|
|
|
{
|
|
|
Regex rx = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
Match match;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
match = rx.Match(strInput);
|
|
|
|
|
|
if (match.Groups[0].Value != null && match.Groups[0].Value.Length > 0)
|
|
|
{
|
|
|
return double.Parse(match.Groups[0].Value);
|
|
|
}
|
|
|
}
|
|
|
catch { }
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="strInput"></param>
|
|
|
/// <param name="strRegex">匹配表达式</param>
|
|
|
/// <returns></returns>
|
|
|
public static List<string> lstStrRegexText(string strInput, string strRegex)
|
|
|
{
|
|
|
Regex rx = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
MatchCollection matches = rx.Matches(strInput);
|
|
|
List<string> lstResult = new List<string>();
|
|
|
|
|
|
foreach (Match matchLocal in matches)
|
|
|
{
|
|
|
lstResult.Add(matchLocal.Groups[0].Value);
|
|
|
}
|
|
|
|
|
|
return lstResult;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="strInput"></param>
|
|
|
/// <param name="strRegex">匹配表达式</param>
|
|
|
/// <returns></returns>
|
|
|
public static List<int> lstIntRegexText(string strInput, string strRegex)
|
|
|
{
|
|
|
Regex rx = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
MatchCollection matches = rx.Matches(strInput);
|
|
|
List<int> lstResult = new List<int>();
|
|
|
|
|
|
foreach (Match matchLocal in matches)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
lstResult.Add(int.Parse(matchLocal.Groups[0].Value));
|
|
|
}
|
|
|
catch { }
|
|
|
}
|
|
|
|
|
|
return lstResult;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="strInput"></param>
|
|
|
/// <param name="strRegex">匹配表达式</param>
|
|
|
/// <returns></returns>
|
|
|
public static List<double> lstDouRegexText(string strInput, string strRegex)
|
|
|
{
|
|
|
Regex rx = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
MatchCollection matches = rx.Matches(strInput);
|
|
|
List<double> lstResult = new List<double>();
|
|
|
|
|
|
foreach (Match matchLocal in matches)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
lstResult.Add(double.Parse(matchLocal.Groups[0].Value));
|
|
|
}
|
|
|
catch { }
|
|
|
}
|
|
|
|
|
|
return lstResult;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 过滤掉无用的字符,只保留“字母、数字、下划线”
|
|
|
/// </summary>
|
|
|
/// <param name="strInput">待过滤的字符串</param>
|
|
|
/// <returns>已过滤过的字符串</returns>
|
|
|
public static string FilterNoUseChar(string strInput)
|
|
|
{
|
|
|
List<string> ret = new List<string>();
|
|
|
Regex reg = new Regex("[A-Za-z0-9_]");
|
|
|
foreach(char c in strInput.ToArray())
|
|
|
{
|
|
|
if(reg.IsMatch(c.ToString()))
|
|
|
{
|
|
|
ret.Add(c.ToString());
|
|
|
}
|
|
|
}
|
|
|
return string.Join("", ret);
|
|
|
}
|
|
|
}
|
|
|
}
|