正则工具-RegexUtil
# 简介
静态工具类 RegexUtil,RegexUtil 是一个C#工具类,提供了多个方法用于正则表达式操作。它能够验证字符串是否与指定的正则表达式匹配,返回匹配结果、所有匹配结果的数组、所有分组的匹配结果的数组、所有分组的匹配结果及分组名称的字典、匹配结果和捕获组名称的字典、所有分组的匹配结果及分组名称的元组等多种格式的返回结果。除此之外,它还能使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串。这些方法可以方便地对文本数据进行处理和解析,提高了开发效率和代码可读性。
# EasyTool.RegexUtil 类
方法说明:
IsMatch
: 验证字符串是否与指定的正则表达式匹配。Match
: 验证字符串是否与指定的正则表达式匹配,并返回匹配结果。Matches
: 验证字符串是否与指定的正则表达式匹配,并返回所有匹配结果的数组。Replace
: 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字- 符MatchGroups
: 验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果- 的数组。MatchGroupsWithNames
: 验证字符串是否与指定的正则表达式匹配,并返回所有分组- 的匹配结果及分组名称的字典。MatchWithGroupNames
: 验证字符串是否与指定的正则表达式匹配,并返回匹配结果和- 捕获组名称的字典。MatchGroupsWithNamesTuple
: 验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果及分组名称的元组。
# IsMatch-验证字符串是否与指定的正则表达式匹配
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>如果字符串与正则表达式匹配,则为true;否则为false</returns>
public static bool IsMatch(string input, string pattern)
# Match-验证字符串是否与指定的正则表达式匹配,并返回匹配结果
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配,并返回匹配结果
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>如果字符串与正则表达式匹配,则为匹配结果;否则为null</returns>
public static string Match(string input, string pattern)
# Matches-验证字符串是否与指定的正则表达式匹配,并返回所有匹配结果
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配,并返回所有匹配结果
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>所有匹配结果的字符串数组</returns>
public static string[] Matches(string input, string pattern)
# Replace-正则表达式匹配替换所有字符串
/// <summary>
/// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串
/// </summary>
/// <param name="input">要替换的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <param name="replacement">替换字符串</param>
/// <returns>替换后的字符串</returns>
public static string Replace(string input, string pattern, string replacement)
/// <summary>
/// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串,并返回替换后的字符串和替换次数
/// </summary>
/// <param name="input">要替换的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <param name="replacement">替换字符串</param>
/// <param name="count">替换次数</param>
/// <returns>包含替换后的字符串和替换次数的元组</returns>
public static (string, int) Replace(string input, string pattern, string replacement, int count)
# MatchGroups-验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>所有分组匹配结果的字符串数组</returns>
public static string[] MatchGroups(string input, string pattern)
# MatchGroupsWithNames-验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果及分组名称
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果及分组名称
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>所有分组匹配结果及分组名称的字典</returns>
public static Dictionary<string, string> MatchGroupsWithNames(string input, string pattern)
# MatchWithGroupNames-验证字符串是否与指定的正则表达式匹配,并返回匹配结果和捕获组名称的字典
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配,并返回匹配结果和捕获组名称的字典
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>匹配结果和捕获组名称的字典</returns>
public static Dictionary<string, string> MatchWithGroupNames(string input, string pattern)
# MatchGroupsWithNamesTuple-验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果及分组名称的元组
/// <summary>
/// 验证字符串是否与指定的正则表达式匹配,并返回所有分组的匹配结果及分组名称的元组
/// </summary>
/// <param name="input">要验证的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <returns>所有分组匹配结果及分组名称的元组</returns>
public static (string, Dictionary<string, string>) MatchGroupsWithNamesTuple(string input, string pattern)
# 代码示例
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
string input = "Hello, world! My name is John Doe. I was born in 2000.";
string pattern = @"\d+";
bool isMatch = RegexUtil.IsMatch(input, pattern);
Console.WriteLine($"Is match: {isMatch}");
string match = RegexUtil.Match(input, pattern);
Console.WriteLine($"Match: {match}");
string[] matches = RegexUtil.Matches(input, pattern);
Console.WriteLine("Matches:");
foreach (string m in matches)
{
Console.WriteLine(m);
}
string replacement = "***";
string replaced = RegexUtil.Replace(input, pattern, replacement);
Console.WriteLine($"Replaced: {replaced}");
(string, int) replacedWithCount = RegexUtil.Replace(input, pattern, replacement, 1);
Console.WriteLine($"Replaced with count: {replacedWithCount.Item1}, count: {replacedWithCount.Item2}");
string inputWithGroups = "John Doe, 20 years old, 180cm tall.";
string patternWithGroups = @"(?<name>\w+)\s+(?<age>\d+)\s+years old,\s+(?<height>\d+)cm tall.";
string[] matchGroups = RegexUtil.MatchGroups(inputWithGroups, patternWithGroups);
Console.WriteLine("Match groups:");
foreach (string g in matchGroups)
{
Console.WriteLine(g);
}
Dictionary<string, string> matchGroupsWithNames = RegexUtil.MatchGroupsWithNames(inputWithGroups, patternWithGroups);
Console.WriteLine("Match groups with names:");
foreach (KeyValuePair<string, string> pair in matchGroupsWithNames)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
Dictionary<string, string> matchWithGroupNames = RegexUtil.MatchWithGroupNames(inputWithGroups, patternWithGroups);
Console.WriteLine("Match with group names:");
foreach (KeyValuePair<string, string> pair in matchWithGroupNames)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
(string, Dictionary<string, string>) matchGroupsWithNamesTuple = RegexUtil.MatchGroupsWithNamesTuple(inputWithGroups, patternWithGroups);
Console.WriteLine($"Match with group names tuple: {matchGroupsWithNamesTuple.Item1}");
foreach (KeyValuePair<string, string> pair in matchGroupsWithNamesTuple.Item2)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
}
}
# 输出结果
Is match: True
Match: 2000
Matches:
2000
Replaced: Hello, world! My name is John Doe. I was born in ***.
Replaced with count: Hello, world! My name is John Doe. I was born in ***., count: 1
Match groups:
John
20
180
Match groups with names:
name: John
age: 20
height: 180
Match with group names:
0: John Doe, 20 years old, 180cm tall.
name: John
age: 20
height: 180
Match with group names tuple: John Doe, 20 years old, 180cm tall.
name: John
age: 20
height: 180
上次更新: 2023/04/26, 22:10:06