●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;

 

转载于:https://www.cnblogs.com/phantom-k/p/5503743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值