系列文章
C#底层库--记录日志帮助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124187709
C#底层库--MySQL脚本自动构建类(insert、update语句生成)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129179216
C#底层库--MySQL数据库访问操作辅助类(推荐阅读)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126886379
C#底层库--XML配置参数读写辅助类(推荐阅读)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129175304
C#底层库--获取文件版本和MD5值
本文链接:https://blog.csdn.net/youcheng_ge/article/details/112513871
C#底层库--操作文件帮助类FileHelper(获取目录的所有文件)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887161
C#底层库--操作Excel帮助类(读取、导出表格)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887445
C#底层库--随机数生成类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126888812
C#底层库--正则表达式帮助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/109745286
C#底层库--CSV和DataTable相互转换
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128804367
C#底层库--Image图片操作类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128805298
C#底层库--JSON帮助类_详细(序列化、反序列化、list、datatable)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128805705
C#底层库--cookie操作辅助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128816347
C#底层库--Session操作辅助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128817096
C#底层库--数据实体类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128816638
C#底层库--Image图片操作类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128805298
C#底层库--数据库类型与程序类型转换类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128817610
C#底层库--数据库类型与程序类型转换类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128817610
C#底层库--字符串操作类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129520428
目录
前言
本专栏为【底层库】,主要介绍编程过程中 通用函数。我们将这些通用固化的源码,进行重写、封装、拓展,再进行单元测试、集成测试、beta测试,最终形成通用化模板,这里我们称为“底层库”。
作为研发人员的你,并不需要花大量时间,研究“底层库”的含义,及“底层库”的实现方法。你只需要几行调用代码,就可以解决项目上碰到的难题。而底层库使用方法,本专栏均有详细介绍,也有项目应用场景。
底层库已实现功能:MySQL脚本构建器、MySQL数据库访问操作、参数配置文件读写、加解密算法、日志记录、HTTP通信、Socket通信、API前后端交互、邮件发送、文件操作、配置参数存储、Excel导入导出、CSV和DataTable转换、压缩解压、自动编号、Session操作等。
本专栏会持续更新,不断优化【底层库】,大家有任何问题,可以私信我。本专栏之间关联性较强(我会使用到某些底层库,某些文章可能忽略介绍),如果您对本专栏感兴趣,欢迎关注,我将带你用最简洁的代码,实现最复杂的功能。
一、底层库作用
本类为正则表达式,用于客户端数据校验。
二、底层库源码
创建类RegexHelper.cs,复制以下代码。
/*
* 由SharpDevelop创建。
* 用户: gyc
* 日期: 2020-11-12
* 时间: 8:18
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Wesun.AssemblyInStock
{
public class RegexHelper
{
/// <summary>
/// 匹配返回List<string>
/// </summary>
/// <param name="a_strPattern">正则表达式</param>
/// <param name="a_txtContext">匹配文本</param>
/// <returns></returns>
public List<string> RegexForList(string a_strPattern,string a_txtContext)
{
List<string> l_listString = new List<string>();
l_listString.Clear();
Regex l_regResult = new Regex(a_strPattern,RegexOptions.IgnoreCase);
MatchCollection matches = l_regResult.Matches(a_txtContext);
foreach(Match item in matches)
{
l_listString.Add(item.Value);
}
return l_listString;
}
/// <summary>
/// 匹配返回Dictionary<string,string>
/// </summary>
/// <param name="a_strPattern">正则表达式</param>
/// <param name="a_txtContext">匹配文本</param>
/// <returns></returns>
public Dictionary<string,string> RegexForDictionary(string a_strPattern,string a_txtContext)
{
Dictionary<string,string> l_dicResult = new Dictionary<string,string>();
l_dicResult.Clear();
Regex reg_Power = new Regex(@a_strPattern,RegexOptions.IgnoreCase);
MatchCollection matches = reg_Power.Matches(a_txtContext);
foreach(Match item in matches)
{
if(l_dicResult.ContainsKey(item.Value)==false)
{
l_dicResult.Add(item.Value,item.Value);
}
}
return l_dicResult;
}
/// <summary>
/// 匹配返回bool
/// </summary>
/// <param name="a_strPattern">正则表达式</param>
/// <param name="a_txtContext">匹配文本</param>
/// <returns></returns>
public bool RegexForBool(string a_strPattern,string a_txtContext)
{
Regex reg = new Regex(@a_strPattern,RegexOptions.IgnoreCase);
Match match = reg.Match(a_txtContext);
return match.Success;
}
/// <summary>
/// 匹配返回string
/// </summary>
/// <param name="a_strPattern">正则表达式</param>
/// <param name="a_txtContext">匹配文本</param>
/// <returns></returns>
public string RegexForString(string a_strPattern,string a_txtContext)
{
Regex l_reg = new Regex(@a_strPattern,RegexOptions.IgnoreCase);
MatchCollection matches_Type = l_reg.Matches(a_txtContext);
foreach(Match item in matches_Type)
{
return item.Value;
}
return "";
}
}
}
三、调用方法
比方说,我编码规则中用到【流水号】字段,我要找到流水号用了几位,要匹配【d位流水号】。
正则表达式:(\[[0-9]+位流水编号])
测试结果: