c# 正则表达式 帮助类

public 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="str"></param>

/// <param name="pattern"></param>

/// <returns></returns>

public MatchCollection Matches(string str, string pattern)

{

Regex reg = new Regex(pattern, RegexOptions.IgnoreCase); // 搜索匹配的字符串

var list = reg.Matches(str);

return list;

}

/// <summary>

/// 正在表达式 拆分字符串

/// </summary>

/// <param name="str"></param>

/// <param name="pattern"></param>

/// <returns></returns>

public string\[\] Split(string str, string pattern)

{

var items = Regex.Split(str, pattern);

return items;

}

/// <summary>

/// 正则替换字符串

/// </summary>

/// <param name="input">要搜索匹配项的字符串</param>

/// <param name="pattern">要匹配的正则表达式模式</param>

/// <param name="replacement">替换字符串</param>

/// <returns></returns>

public string Replace(string input, string pattern, string replacement)

{

var str = Regex.Replace(input, pattern, replacement);

return str;

}

}

相关推荐
TimberWill13 小时前
windows终端分屏设置
windows
唐青枫15 小时前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
不吃鱼的羊16 小时前
DTC错误状态异常排查
windows
fogota17 小时前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
灸木18 小时前
C# 多线程与异步
c#
hoaxxcj21 小时前
DeepSeek Harness 本地部署与第三方插件排障实录:从 fetch failed 到 preset not found
人工智能·windows·开源·ai agent·deepseek
鱼听禅1 天前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
梦帮科技1 天前
Next.js 16 模块化单体实战:App Router、领域模块与 Route Handler 分层
css·数据结构·链表·正则表达式·node.js·json·html5
江湖人称菠萝包1 天前
【Windows】《深入浅出Windows API程序设计:核心编程篇》笔记-Chapter11-PE文件格式深入剖析
windows·笔记
geovindu1 天前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式