C#中正则表达式的使用实例
2013-01-17 13:48
267 查看
C#中正则表达式的使用
/// <summary> /// 判断修改数据的格式是否符合要求 /// </summary> /// <param name="strIsTimLim"></param> /// <param name="strBeginTime"></param> /// <param name="strEndTime"></param> /// <param name="strCalender"></param> /// <returns></returns> private bool isPatternValid(string m_strIsTimLim, string m_strBeginTime, string m_strEndTime, string m_strCalender) { bool isPatternValid = false; string patternIsTimLim = @"^[Y|y|N|n]{1}$";//匹配只能输入Y或y或n或N的其中一个 string patternDate = @"^\d{4}[/]([0][1-9]|(1[0-2]))[/]([1-9]|([012]\d)|(3[01]))([ \t\n\x0B\f\r])(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))([:])(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))([:])((([0-5]{1}[0-9]{1}|[6]{1}[0]{1})))$";//匹配格式如2012/12/15 18:00:00 string patternCalender = @"^\d{4}[-]\d{4}[-]([1|2])$";//匹配年度格式,如:2012-2013-1,这个正则表达式有不完整的地方 //string temp = "2102/12/3 1:22:22"; //Match tempmatch = Regex.Match(temp, patternDate); //bool tempb =tempmatch.Success; Match m_patternIsTimLim = Regex.Match(m_strIsTimLim, patternIsTimLim); // 匹配正则表达式,需要添加:using System.Text.RegularExpressions; Match m_patternBeginDate = Regex.Match(m_strBeginTime, patternDate); Match m_patternEndDate = Regex.Match(m_strEndTime, patternDate); Match m_patternCalender = Regex.Match(m_strCalender, patternCalender); if (m_patternIsTimLim.Success && m_patternBeginDate.Success && m_patternEndDate.Success && m_patternCalender.Success)//序号匹配成功 { isPatternValid = true; } else if (!m_patternIsTimLim.Success) {//匹配失败 isPatternValid = false; MessageBox.Show("只能输入Y或y或n或N中的一个字母", "警告", MessageBoxButtons.OKCancel); } else if ((!m_patternBeginDate.Success) || (!m_patternEndDate.Success)) { isPatternValid = false; MessageBox.Show("时间格式:2012/12/15 18:00:00", "警告", MessageBoxButtons.OKCancel); } else if (!m_patternCalender.Success) { isPatternValid = false; MessageBox.Show("如:2012-2013-1或如:2012-2013-2", "警告", MessageBoxButtons.OKCancel); } return isPatternValid; }
相关文章推荐
- 实例分析js和C#中使用正则表达式匹配a标签
- c#中正则表达式使用实例复习
- 使用C#的正则表达式验证中文字符(实例代码)
- 使用C#的正则表达式验证中文字符(实例代码)
- 实例分析js和C#中使用正则表达式匹配a标签
- C#使用正则表达式实例
- C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
- C#正则表达式使用介绍
- 正则表达式在.Net中的使用(C#)
- C#中正则表达式的使用
- C#中使用正则表达式验证特殊字符
- [C#][固定格式网页解析]使用正则表达式处理网页的初步体会
- java正则表达式语法详解及其使用代码实例
- C#正则表达式的使用(Regex类用法)
- C# .Net使用正则表达式去除HTML标记和空格
- C#中使用正则表达式验证Email格式、IP地址格式和URL网址格式
- C#使用正则表达式过滤html标签
- java正则表达式语法详解及其使用代码实例
- C#中如何使用正则表达式