zoukankan      html  css  js  c++  java
  • 正则表达式测试器

    程序源自:<<C#字符串和正则表达式参考手册>>

    这个例子程序很实用,可以把这个程序做为一个小工具来使用,很方便

    以下是这个程序的界面.

    以下是核心代码:

    代码
      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.IO;
     10 using System.Text.RegularExpressions;
     11 
     12 namespace RegexTester
     13 {
     14     public partial class RegexTesterForm : Form
     15     {
     16         public RegexTesterForm()
     17         {
     18             InitializeComponent();
     19         }
     20 
     21         #region 打开与存储正则表达式
     22 
     23         private void SaveRegexButton_Click(object sender, EventArgs e)
     24         {
     25             saveFileDialog1.ShowDialog();
     26         }
     27 
     28         private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
     29         {
     30             StreamWriter swRegex = File.CreateText(saveFileDialog1.FileName);
     31             swRegex.Write(this.RegexTextBox.Text);
     32             swRegex.Close();
     33         }
     34 
     35         private void OpenRegexButton_Click(object sender, EventArgs e)
     36         {
     37             openFileDialog1.ShowDialog();
     38         }
     39 
     40         private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
     41         {
     42             StreamReader srRegex = File.OpenText(openFileDialog1.FileName);
     43             this.RegexTextBox.Text = srRegex.ReadToEnd();
     44             srRegex.Close();
     45         }
     46 
     47         #endregion
     48 
     49         /// <summary>
     50         /// 得到正则表达式选项
     51         /// </summary>
     52         /// <returns>返回正则表达式选项</returns>
     53         private RegexOptions GetSelectedRegexOptions()
     54         {
     55             RegexOptions selectedRegexOption = RegexOptions.None;
     56             if (this.IgnoreCaseCheckBox .Checked )
     57             {
     58                 selectedRegexOption |=RegexOptions .IgnoreCase ;
     59             }
     60             if (this.ExplicitCaptureCheckBox  .Checked )
     61             {
     62                 selectedRegexOption |=RegexOptions.ExplicitCapture  ;
     63             }
     64             if (this.ECMAScriptCheckBox .Checked )
     65             {
     66                 selectedRegexOption |= RegexOptions.ECMAScript ;
     67             }
     68             if (this.IgnorePatternWhiteSpaceCheckBox .Checked)
     69             {
     70                 selectedRegexOption |= RegexOptions.IgnorePatternWhitespace ;
     71             }
     72             if (this.MultiLineCheckBox .Checked)
     73             {
     74                 selectedRegexOption |= RegexOptions.Multiline;
     75             }
     76 
     77             if (this.RightToLeftCheckBox.Checked)
     78             {
     79                 selectedRegexOption |= RegexOptions.RightToLeft ;
     80             }
     81             if (this.SingleLineCheckBox .Checked)
     82             {
     83                 selectedRegexOption |= RegexOptions.Singleline ;
     84             }
     85 
     86             return selectedRegexOption;
     87 
     88         }
     89 
     90         private void TestRegexButton_Click(object sender, EventArgs e)
     91         {
     92             try
     93             {
     94                 RegexOptions selectedRegexOptions =
     95                     this.GetSelectedRegexOptions();
     96                 Regex testRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
     97                 if (testRegex .IsMatch (this.InputTextBox .Text ))
     98                 {
     99                     this.ResultsTextBox.ForeColor = Color.Black;
    100                     this.ResultsTextBox.Text = "字符串与正则表达式有匹配项!";
    101                 }
    102                 else
    103                 {
    104                     this.ResultsTextBox.ForeColor = Color.Red;
    105                     this.ResultsTextBox.Text = "字符串与正则表达式无匹配项!";
    106                 }
    107             }
    108             catch (ArgumentException ex )
    109             {
    110                 this.ResultsTextBox.ForeColor = Color.Red;
    111                 this.ResultsTextBox.Text = "There waw an error in your Regular Expression:\r\n"
    112                     + ex.Message;
    113                 
    114             }
    115         }
    116 
    117         private void ReplaceButton_Click(object sender, EventArgs e)
    118         {
    119             try
    120             {
    121                 RegexOptions selectedRegexOption = this.GetSelectedRegexOptions();
    122                 Regex replaceRegex = new Regex(this.RegexTextBox.Text, selectedRegexOption);
    123                 this.RegexTextBox.ForeColor = Color.Black;
    124                 this.ResultsTextBox.Text = replaceRegex.Replace(
    125                     this.InputTextBox.Text, this.ReplacementTextBox.Text);
    126  
    127             }
    128             catch (ArgumentException ex )
    129             {
    130                 this.ResultsTextBox.ForeColor = Color.Red;
    131                 this.ResultsTextBox.Text = "There is an error in"
    132                     + "your regular expression" + ex.Message;
    133             }
    134         }
    135 
    136         private void SpiltButton_Click(object sender, EventArgs e)
    137         {
    138             try
    139             {
    140                             RegexOptions selectedRegexOption = this.GetSelectedRegexOptions();
    141             Regex spiltRegex = new Regex(this.RegexTextBox.Text, selectedRegexOption);
    142 
    143             string[] spiltResults;
    144             spiltResults = spiltRegex.Split(
    145                 this.InputTextBox.Text);
    146             StringBuilder resultsString = new StringBuilder(this.InputTextBox.Text.Length);
    147             foreach (string item in spiltResults )
    148             {
    149                 resultsString.Append(item + Environment.NewLine);
    150             }
    151             this.ResultsTextBox.Text = resultsString.ToString();
    152             }
    153             catch (ArgumentException ex )
    154             {
    155                 this.ResultsTextBox.ForeColor = Color.Red;
    156                 this.ResultsTextBox.Text = "There is an error in "
    157                     + "this regular expression\r\n" + ex.Message;
    158             }
    159         }
    160 
    161         private void MatchesButton_Click(object sender, EventArgs e)
    162         {
    163             try
    164             {
    165                 RegexOptions selectedRegexOption = this.GetSelectedRegexOptions();
    166                 Regex matchRegex = new Regex(this.RegexTextBox.Text, selectedRegexOption);
    167 
    168                 MatchCollection matchesFound;
    169 
    170                 matchesFound = matchRegex.Matches(this.InputTextBox.Text);
    171                 string NextMatch = "-------------NEXT MATCH------------\r\n";
    172                 StringBuilder resultString = new StringBuilder(64);
    173 
    174                 foreach (Match  item in matchesFound )
    175                 {
    176                     resultString.Append(item.Value + Environment.NewLine + NextMatch);
    177                 }
    178                 this.ResultsTextBox.Text = resultString.ToString();
    179             }
    180             catch (ArgumentException ex)
    181             {
    182                 this.ResultsTextBox.ForeColor = Color.Red;
    183                 this.ResultsTextBox.Text = "There is an error in"
    184                     + "your regular expression" + ex.Message;
    185             }
    186         }
    187     }
    188 }
    189 

    要源程序的联系:hbhbice@gmail.com

  • 相关阅读:
    Minikube体验
    基于Vue、Bootstrap的Tab形式的进度展示
    Python 网络请求模块 urllib 、requests
    SRE学习笔记:分布式共识系统、Paxos协议
    ReactNative: 使用对话框组件AlertIOS组件
    ReactNative: 使用AsyncStorage异步存储类
    ReactNative: 使用网页组件WebView组件
    ReactNative: 使用标签栏组件TabBarIOS组件
    ReactNative: 使用像素密度类PixelRatio进行适配
    ReactNative: 使用尺寸类Dimensions获取屏幕尺寸
  • 原文地址:https://www.cnblogs.com/hbhbice/p/1801321.html
Copyright © 2011-2022 走看看