C#正则表达式

来源:互联网 发布:血源诅咒dlc武器数据 编辑:程序博客网 时间:2024/05/21 23:40

没用到多少正则的判断,有点懒,闲着写的。主要写java的时候会具体,C#不想弄了。

1、画窗体:


2、实现判断类,其中写入错误日志,暂时可忽略。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Windows.Forms;namespace XML_Operation{    class RegexUtils    {        //目标字符串        private string sTarget = string.Empty;        //正则字符串        private string sRegex = string.Empty;        /// <summary>        /// 构造函数        /// </summary>        /// <param name="sTarget">目标字符串</param>        /// <param name="sRegex">正则字符串</param>        public RegexUtils(string sTarget, string sRegex)        {            this.sRegex = sRegex;            this.sTarget = sTarget;        }        /// <summary>        /// 判断是否匹配        /// </summary>        /// <returns>匹配返回True,不匹配返回False</returns>        public bool DoRegex(string sLog)        {            if (string.IsNullOrEmpty(sRegex))                return true;            try            {                Match match = Regex.Match(sTarget, sRegex);                return match.Success;            }            catch (System.Exception ex)            {                <span style="color:#ff0000;">LogFile.writeLog(sLog, ex.Message);</span>                return false;            }        }    }}

2、添加装饰类,对字符串的合法性进行判断。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Windows.Forms;namespace XML_Operation{    class RegexDecoration    {        /// <summary>        /// 判断正则表达式是否成立        /// </summary>        /// <param name="sTarget">源字符串</param>        /// <param name="sRegex">正则表达式字符串</param>        public static void JudgeRegex(string sTarget, string sRegex)        {            if (string.IsNullOrEmpty(sRegex))            {                MessageBox.Show("请填写【正则表达式】", "【正则表达式】", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            string sLog = "正则表达式";            RegexUtils regexUtil = new RegexUtils(sTarget, sRegex);            if (regexUtil.DoRegex(sLog))            {                MessageBox.Show("匹配成功", "【正则表达式】", MessageBoxButtons.OK, MessageBoxIcon.Information);                return;            }            else            {                DialogResult dr = MessageBox.Show("匹配失败,可能出现异常,是否查看日志?", "【正则表达式】", MessageBoxButtons.YesNo, MessageBoxIcon.Information);                if (dr == DialogResult.Yes)                {                    if(File.Exists(sLog))                    LogFile.showLog(sLog);                }                return;            }        }    }}
3、实现日志操作类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace XML_Operation{    class LogFile    {        /// <summary>        /// 写错误日志        /// </summary>        /// <param name="LogFileName">日志文件名</param>        /// <param name="sErrorInformation">日志错误信息</param>        public static void writeLog(string LogFileName, string sErrorInformation)        {            if (File.Exists(LogFileName))            {                File.AppendAllText(LogFileName, sErrorInformation);            }            else            {                File.WriteAllText(LogFileName, sErrorInformation);            }        }        /// <summary>        /// 查看日志文件        /// </summary>        /// <param name="LogFileName">日志文件名</param>        public static void showLog(string LogFileName)        {            System.Diagnostics.Process.Start(LogFileName);        }    }}

具体源码下载地址:http://download.csdn.net/detail/anlidengshiwei/8440419点击打开链接

其中包含一个以前做的XML文件操作,不想删除了,直接上传了。自己好懒啊!

0 0