zoukankan      html  css  js  c++  java
  • ●c#使用正则表达式

    using System.Text.RegularExpressions;
    
                string drugComment = "药品价格及供应商:1.3,0001,药品供应商; 其他";  //字符串格式示例:价格,供应商代码,供应商名称
                
    //正则截取
                Regex regSupply = new Regex(@"供应商代码(d*)");  //定义供应商正则表达式
                Match matSupply = regSupply.Match(drugComment);  //根据正则表达式,从字符串中截取供应商信息
    
                string matSupplyCode = matSupply.Groups[0].ToString();
                if (matSupplyCode.Length > 7)  //如果有供货商代码,需要验证
                {
                    string strSupplyCode = matSupplyCode.Substring(6, matSupplyCode.Length - 7);  //截取供应商代码
                }
                else
                    new CustMsgBoxWindow().Show(msg: "区域药品备注中无供应商代码!");
                        
    //正则匹配
                Regex reg = new Regex(@"^药品价格及供应商:(d{1,}.d{1,}|d{1,}),d{1,},S{1,};"); //用正则表达式验证药品备注中的进价及供应商格式
                if (reg.IsMatch(drugComment))
                {
                    string strYPBZ = drugComment.Substring(drugComment.IndexOf(";") + 1);
                    string strPriceAddSupply = drugComment.Substring(drugComment.IndexOf(":") + 1, drugComment.IndexOf(";") - drugComment.IndexOf(":") - 1);
                    string[] strPAS = strPriceAddSupply.Split(',');
                }
                else
                    tbYPBZ.Text = drugComment;
    
    //正则替换
                Regex reg = new Regex(@"^药品价格及供应商((d*.d*|d*),d*,S*);"); //用正则表达式验证药品备注中的进价及供应商格式
                if (reg.IsMatch(strYPBZ))
                    strYPBZ = reg.Replace(strYPBZ, strPriceAddSupply);  //strPriceAddSupply替换strYPBZ中符合格式的字符串片段
                else
                    strYPBZ = strPriceAddSupply + "  " + strYPBZ;
    
                tbYPBZ.Text = strYPBZ;
  • 相关阅读:
    C#中提供的精准测试程序运行时间的类Stopwatch
    [转]SQLite数据库扫盲
    [转]c# 使用ChartDirector绘图的一些个人体会
    [转]SQLite内存数据库
    SQL Server 各种查询语句执行返回结果
    [转]浅谈 BigInteger
    [转]SQLite数据库连接方式
    ASP.NET 3.5 开发大全DOC版
    好像昨天不见了100块钱。
    热烈庆祝本人昨天终于申请得了google ad
  • 原文地址:https://www.cnblogs.com/phantom-k/p/5503743.html
Copyright © 2011-2022 走看看