●c#使用正则表达式
2016-05-17 23:49
309 查看
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# 委托(Delegate Action Func Predicate)
- C#系统硬件类列表
- NetAnalyzer笔记 之 十 贫民的选择 —— 通过邮件方式打造自己的bug反馈模块(C#)
- Quartz.NET c# 教程 - 课程三:Jobs 和 JobDetails
- C#装箱和拆箱(Boxing 和 UnBoxing)
- C#Winform判断文件和路径是否存在
- WCF服务寄宿IIS与Windows服务 - C#/.NET
- c# DataTable遍历
- C#当前日期时间
- C# 转换时间戳
- 第3章 C# 程序的流程控制
- C#学习笔记
- c#打包安装程序默认安装路径设置
- C#:绘制Winform窗体
- C#视频播放器
- C# 自定义控件,自定义属性,自定义事件
- C#中ICollection介绍
- C#学习笔记之调用动态库
- 第2章 C#程序设计基础
- C#向无窗口的进程发送消息