C#底层库--RegexHelper正则表达式辅助类

系列文章

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]+位流水编号])

测试结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花北城

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值