跳转到内容

RegexHelper

RegexHelper 是一个静态帮助类,为正则表达式操作提供了丰富的功能,包括基础验证、格式验证、匹配操作、文本替换、信息提取、格式化等,旨在简化常见的正则表达式应用场景开发,提高代码的可读性和可维护性。

该类预定义了以下常用正则表达式模式常量:

常量名说明模式示例
EmailPattern电子邮件^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
MobilePhonePattern中国大陆手机号^1[3-9]\d{9}$
TelephonePattern固定电话^(\d{3,4}-)?\d{7,8}(-\d{1,4})?$
IdCardPattern身份证号码(15位或18位)18位含校验码
UrlPatternURL地址^(https?|ftp)://[^\s/$.?#].[^\s]*$
IPv4PatternIPv4地址标准 IPv4 格式
IPv6PatternIPv6地址标准 IPv6 格式
IntegerPattern整数^-?\d+$
PositiveIntegerPattern正整数^[1-9]\d*$
FloatPattern浮点数^-?\d+(\.\d+)?$
ChinesePattern中文字符^[\u4e00-\u9fa5]+$
LetterPattern字母^[a-zA-Z]+$
AlphanumericPattern字母数字^[a-zA-Z0-9]+$
PostalCodePattern中国邮政编码^[1-9]\d{5}$
BankCardPattern银行卡号^\d{16,19}$
LicensePlatePattern中国车牌号含新能源车牌
DatePattern日期(yyyy-MM-dd)^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
TimePattern时间(HH:mm:ss)^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$
HexColorPattern十六进制颜色^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
QQPatternQQ号^[1-9]\d{4,10}$
WeChatPattern微信号^[a-zA-Z][a-zA-Z0-9_-]{5,19}$
UsernamePattern用户名^[a-zA-Z][a-zA-Z0-9_]{4,15}$
StrongPasswordPattern强密码至少8位,含大小写字母和数字

提供常见数据格式的验证功能,确保输入数据符合预期格式。

主要方法:

验证字符串是否匹配指定正则表达式。

public static bool IsMatch(string? input, string pattern, RegexOptions options = RegexOptions.None)

参数:

  • input: 输入字符串
  • pattern: 正则表达式模式
  • options: 正则表达式选项

返回值:

  • 是否匹配

示例:

bool isValid = RegexHelper.IsMatch("test@example.com", RegexHelper.EmailPattern);

验证是否为有效的电子邮件地址。

public static bool IsValidEmail(string? email)

示例:

bool isValid = RegexHelper.IsValidEmail("test@example.com");
Console.WriteLine($"邮箱有效: {isValid}");

验证是否为有效的手机号码(中国大陆)。

public static bool IsValidMobilePhone(string? mobile)

示例:

bool isValid = RegexHelper.IsValidMobilePhone("13812345678");
Console.WriteLine($"手机号有效: {isValid}");

验证是否为有效的身份证号码(15位或18位),包含校验码验证。

public static bool IsValidIdCard(string? idCard)

示例:

bool isValid = RegexHelper.IsValidIdCard("110101199001011234");
Console.WriteLine($"身份证有效: {isValid}");

IsValidUrl / IsValidIPv4 / IsValidIPv6 / IsValidIP

Section titled “IsValidUrl / IsValidIPv4 / IsValidIPv6 / IsValidIP”

验证 URL、IPv4、IPv6 或 IP 地址。

public static bool IsValidUrl(string? url)
public static bool IsValidIPv4(string? ip)
public static bool IsValidIPv6(string? ip)
public static bool IsValidIP(string? ip)

验证固定电话号码。

public static bool IsValidTelephone(string? telephone)

示例:

bool isValidUrl = RegexHelper.IsValidUrl("https://www.example.com");
bool isValidIPv4 = RegexHelper.IsValidIPv4("192.168.1.1");

IsValidPostalCode / IsValidBankCard / IsValidLicensePlate

Section titled “IsValidPostalCode / IsValidBankCard / IsValidLicensePlate”

验证邮政编码、银行卡号、车牌号。

public static bool IsValidPostalCode(string? postalCode)
public static bool IsValidBankCard(string? bankCard)
public static bool IsValidLicensePlate(string? licensePlate)

IsValidQQ / IsValidWeChat / IsValidHexColor

Section titled “IsValidQQ / IsValidWeChat / IsValidHexColor”

验证 QQ号、微信号、十六进制颜色值。

public static bool IsValidQQ(string? qq)
public static bool IsValidWeChat(string? wechat)
public static bool IsValidHexColor(string? color)

提供数据格式类型的验证功能。

主要方法:

IsInteger / IsPositiveInteger / IsFloat / IsNumber

Section titled “IsInteger / IsPositiveInteger / IsFloat / IsNumber”

验证整数、正整数、浮点数、数字。

public static bool IsInteger(string? input)
public static bool IsPositiveInteger(string? input)
public static bool IsFloat(string? input)
public static bool IsNumber(string? input)

示例:

bool isInt = RegexHelper.IsInteger("123");
bool isFloat = RegexHelper.IsFloat("123.45");
Console.WriteLine($"是整数: {isInt}");
Console.WriteLine($"是浮点数: {isFloat}");

验证中文、字母、字母数字。

public static bool IsChinese(string? input)
public static bool IsLetter(string? input)
public static bool IsAlphanumeric(string? input)

示例:

bool isChinese = RegexHelper.IsChinese("你好世界");
Console.WriteLine($"是中文: {isChinese}");

IsValidDate / IsValidTime / IsValidDateTime

Section titled “IsValidDate / IsValidTime / IsValidDateTime”

验证日期、时间、日期时间格式。

public static bool IsValidDate(string? input)
public static bool IsValidTime(string? input)
public static bool IsValidDateTime(string? input)

IsValidUsername / IsStrongPassword / GetPasswordStrength

Section titled “IsValidUsername / IsStrongPassword / GetPasswordStrength”

验证用户名、密码强度。

public static bool IsValidUsername(string? username)
public static bool IsStrongPassword(string? password)
public static int GetPasswordStrength(string? password)

示例:

int strength = RegexHelper.GetPasswordStrength("Abc123!@#");
Console.WriteLine($"密码强度: {strength}");

验证字符串长度范围、空白字符。

public static bool IsLengthInRange(string? input, int minLength, int maxLength)
public static bool IsWhitespace(string? input)

ContainsChinese / ContainsDigit / ContainsLetter / ContainsSpecialChar

Section titled “ContainsChinese / ContainsDigit / ContainsLetter / ContainsSpecialChar”

检查是否包含中文、数字、字母、特殊字符。

public static bool ContainsChinese(string? input)
public static bool ContainsDigit(string? input)
public static bool ContainsLetter(string? input)
public static bool ContainsSpecialChar(string? input)

提供文本中匹配特定模式内容的功能,返回匹配结果。

主要方法:

获取第一个匹配结果。

public static Match? Match(string? input, string pattern, RegexOptions options = RegexOptions.None)

参数:

  • input: 输入字符串
  • pattern: 正则表达式模式
  • options: 正则表达式选项

返回值:

  • 匹配结果,如果没有匹配则返回 null

示例:

var match = RegexHelper.Match("Hello 123 World", @"\d+");
if (match != null)
{
Console.WriteLine($"匹配到: {match.Value}");
}

获取所有匹配结果。

public static MatchCollection Matches(string? input, string pattern, RegexOptions options = RegexOptions.None)

示例:

var matches = RegexHelper.Matches("Hello 123 World 456", @"\d+");
foreach (Match match in matches)
{
Console.WriteLine($"匹配到: {match.Value}");
}

获取所有匹配的字符串值。

public static List<string> GetMatchValues(string? input, string pattern, RegexOptions options = RegexOptions.None)

示例:

var values = RegexHelper.GetMatchValues("Hello 123 World 456", @"\d+");
// values = ["123", "456"]

GetGroupValue / GetGroupValues / GetAllGroupValues

Section titled “GetGroupValue / GetGroupValues / GetAllGroupValues”

获取匹配的分组值。

public static string? GetGroupValue(string? input, string pattern, string groupName, RegexOptions options = RegexOptions.None)
public static List<string> GetGroupValues(string? input, string pattern, string groupName, RegexOptions options = RegexOptions.None)
public static Dictionary<string, string> GetAllGroupValues(string? input, string pattern, RegexOptions options = RegexOptions.None)

示例:

var phone = "13812345678";
var pattern = @"(?<prefix>1[3-9])(?<number>\d{8})";
var prefix = RegexHelper.GetGroupValue(phone, pattern, "prefix");
Console.WriteLine($"前缀: {prefix}");

提供文本内容的替换功能,包括信息脱敏和格式化等。

主要方法:

替换所有匹配的字符串。

public static string Replace(string? input, string pattern, string replacement, RegexOptions options = RegexOptions.None)
public static string Replace(string? input, string pattern, MatchEvaluator evaluator, RegexOptions options = RegexOptions.None)

参数:

  • input: 输入字符串
  • pattern: 正则表达式模式
  • replacement: 替换字符串 或 回调函数

返回值:

  • 替换后的字符串

示例:

var result = RegexHelper.Replace("Hello 123 World", @"\d+", "XXX");
// result = "Hello XXX World"
var result2 = RegexHelper.Replace("Hello 123 World 456", @"\d+", match =>
{
return $"[{match.Value}]";
});
// result2 = "Hello [123] World [456]"

移除所有匹配的字符串。

public static string Remove(string? input, string pattern, RegexOptions options = RegexOptions.None)

示例:

var result = RegexHelper.Remove("Hello 123 World 456", @"\d+");
// result = "Hello World "

RemoveHtmlTags / RemoveWhitespace / CompressWhitespace

Section titled “RemoveHtmlTags / RemoveWhitespace / CompressWhitespace”

移除 HTML 标签、空白字符或压缩空白。

public static string RemoveHtmlTags(string? input)
public static string RemoveWhitespace(string? input)
public static string CompressWhitespace(string? input)

示例:

var result = RegexHelper.RemoveHtmlTags("<p>Hello</p>");
// result = "Hello"
var result2 = RegexHelper.CompressWhitespace("Hello World");
// result2 = "Hello World"

条件替换:仅当匹配时替换。

public static string ReplaceIfMatch(string? input, string pattern, string replacement, RegexOptions options = RegexOptions.None)

提供从文本中提取特定信息的功能。

主要方法:

ExtractNumbers / ExtractIntegers / ExtractDoubles

Section titled “ExtractNumbers / ExtractIntegers / ExtractDoubles”

提取数字、整数、浮点数。

public static List<string> ExtractNumbers(string? input)
public static List<int> ExtractIntegers(string? input)
public static List<double> ExtractDoubles(string? input)

示例:

var numbers = RegexHelper.ExtractNumbers("Price: $123.45, Tax: $67.89");
// numbers = ["123", "45", "67", "89"]
var integers = RegexHelper.ExtractIntegers("Price: $123, Tax: $67");
// integers = [123, 67]

提取所有邮箱地址。

public static List<string> ExtractEmails(string? input)

示例:

var emails = RegexHelper.ExtractEmails("Contact: test@example.com or support@example.org");
// emails = ["test@example.com", "support@example.org"]

提取所有手机号码。

public static List<string> ExtractMobilePhones(string? input)

提取所有 URL。

public static List<string> ExtractUrls(string? input)

提取所有 IPv4 地址。

public static List<string> ExtractIPv4Addresses(string? input)

提取所有日期。

public static List<string> ExtractDates(string? input)

提取所有中文字符。

public static List<string> ExtractChinese(string? input)

提取所有英文单词。

public static List<string> ExtractWords(string? input)

提取第一个匹配的字符串。

public static string? ExtractFirst(string? input, string pattern, RegexOptions options = RegexOptions.None)

提供文本格式化功能,改善文本显示效果。

主要方法:

对字符串进行脱敏处理。

public static string Mask(string? input, int keepStart, int keepEnd)

参数:

  • input: 输入字符串
  • keepStart: 保留开头字符数
  • keepEnd: 保留结尾字符数

示例:

var masked = RegexHelper.Mask("13812345678", 3, 4);
// masked = "138****5678"

HideMobilePhone / HideEmail / HideIdCard / HideBankCard

Section titled “HideMobilePhone / HideEmail / HideIdCard / HideBankCard”

对手机号、邮箱、身份证、银行卡进行脱敏。

public static string? HideMobilePhone(string? phone)
public static string? HideEmail(string? email)
public static string? HideIdCard(string? idCard)
public static string? HideBankCard(string? bankCard)

示例:

var maskedPhone = RegexHelper.HideMobilePhone("13812345678");
// maskedPhone = "138****5678"
var maskedEmail = RegexHelper.HideEmail("test@example.com");
// maskedEmail = "t***@example.com"
var maskedIdCard = RegexHelper.HideIdCard("110101199001011234");
// maskedIdCard = "110101********1234"

格式化手机号(添加分隔符)。

public static string? FormatMobilePhone(string? phone)

示例:

var formatted = RegexHelper.FormatMobilePhone("13812345678");
// formatted = "138-1234-5678"

格式化银行卡号(添加空格分隔)。

public static string? FormatBankCard(string? bankCard)

示例:

var formatted = RegexHelper.FormatBankCard("6222021234567890123");
// formatted = "6222 0212 3456 7890 123"

ToCamelCase / ToPascalCase / ToSnakeCase / ToKebabCase

Section titled “ToCamelCase / ToPascalCase / ToSnakeCase / ToKebabCase”

命名风格转换。

public static string ToCamelCase(string? input)
public static string ToPascalCase(string? input)
public static string ToSnakeCase(string? input)
public static string ToKebabCase(string? input)

示例:

var camel = RegexHelper.ToCamelCase("user_name"); // "userName"
var pascal = RegexHelper.ToPascalCase("user_name"); // "UserName"
var snake = RegexHelper.ToSnakeCase("userName"); // "user_name"
var kebab = RegexHelper.ToKebabCase("userName"); // "user-name"

使用正则表达式分割字符串。

public static string[] Split(string? input, string pattern, RegexOptions options = RegexOptions.None)

测试正则表达式并返回详细结果。

public static RegexTestResult Test(string? input, string pattern, RegexOptions options = RegexOptions.None)

验证正则表达式模式是否有效。

public static bool ValidatePattern(string? pattern, out string? errorMessage)

转义/反转义正则表达式特殊字符。

public static string Escape(string? input)
public static string Unescape(string? input)

计算匹配次数。

public static int Count(string? input, string pattern, RegexOptions options = RegexOptions.None)
  1. 表单数据验证 - 验证用户输入的邮箱、手机号、身份证等信息格式
  2. 数据清洗处理 - 从非结构化的文本中提取有用信息
  3. 敏感信息脱敏 - 对手机号、邮箱、身份证等进行脱敏处理
  4. 文本格式化 - 统一文本格式,改善显示效果
  5. 数据过滤 - 移除HTML标签、特殊字符等不需要的内容
  6. 数据解析 - 从日志、网页文本中提取特定信息
  7. 命名规范转换 - 对变量名进行格式转换
  8. 网页内容处理 - 从页面源码中提取链接、价格等信息
  1. 所有方法对空值和空字符串进行了安全处理,不会抛出异常
  2. 身份证验证包含中国标准校验码验证
  3. 手机号验证仅支持中国大陆手机号
  4. 当匹配不到结果时,返回空列表或 null
  5. 提取方法在匹配失败时返回空字符串
  6. 正则表达式模式为静态常量,不会影响性能
  7. 验证 URL 方法为通用格式,无法验证链接是否真实存在
  8. 身份证验证仅验证格式和校验码,不验证日期的真实有效性
  9. 金额验证支持最多两位小数