凌波星子

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

这两天学习了C#正则表达式,网址:http://www.wangqi.com/n9250c53.aspx

根据上面的指导,自己做了个正则表达式验证器

界面xmal文件如下:

 1 <Grid x:Name="LayoutRoot">
 2         <Grid.RowDefinitions>
 3             <RowDefinition Height="25"/>
 4             <RowDefinition Height="0.3*"/>
 5             <RowDefinition Height="60"/>
 6             <RowDefinition Height="25"/>
 7             <RowDefinition Height="0.3*"/>
 8             <RowDefinition Height="25"/>
 9             <RowDefinition Height="0.19*"/>
10             <RowDefinition Height="30"/>
11         </Grid.RowDefinitions>
12         <Label Content="正则表达式代码" Grid.Row="0"/>
13         <ScrollViewer Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
14             <TextBox x:Name="TextBox_RegexTemplate"/>
15         </ScrollViewer>
16         <GroupBox Header="表达式设置" Grid.Row="2">
17             <StackPanel>
18                 <StackPanel Orientation="Horizontal">
19                     <CheckBox Content="忽略大小写" x:Name="CheckBox_IgnoreCase" Margin="5,0,0,0"/>
20                     <CheckBox Content="消除模式空白处理" x:Name="CheckBox_None" Margin="10,0,0,0"/>
21                     
22                 </StackPanel>
23                 <StackPanel Orientation="Horizontal">
24                     <CheckBox Content="右至左" x:Name="CheckBox_RightToLeft" Margin="5,0,0,0"/>
25                     <CheckBox Content="多行模式" x:Name="CheckBox_MultiLine" Margin="10,0,0,0"/>
26                     <CheckBox Content="单行模式" x:Name="CheckBox_SingleLine" Margin="10,0,0,0"/>
27                 </StackPanel>
28             </StackPanel>
29         </GroupBox>
30         <Label Grid.Row="3" Content="验证字符串"/>
31         <ScrollViewer Grid.Row="4" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
32             <TextBox x:Name="TextBox_RegexTest"/>
33         </ScrollViewer>
34         <Label Grid.Row="5" Content="验证结果"/>
35         <ScrollViewer Grid.Row="6" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
36             <Border BorderBrush="Black" BorderThickness="0.35">
37                 <TextBox IsReadOnly="True" Background="#FFE5DEDE" x:Name="TextBox_Result" />
38             </Border>
39         </ScrollViewer>
40         <Button Grid.Row="7" Content="IsMatch()验证" Click="Click_IsMatch"  Height="25" Width="100" HorizontalAlignment="Left" Margin="20,0,0,0"/>
41         <Button Grid.Row="7" Content="清空" Click="Click_Clear" Height="25" Width="100" Margin="150,0,0,0" />
42     </Grid>

后台处理函数代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Windows;
 5 using System.Windows.Controls;
 6 using System.Windows.Data;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Imaging;
11 using System.Windows.Shapes;
12 using System.Text.RegularExpressions;
13 
14 namespace 正则法则学习
15 {
16     /// <summary>
17     /// MainWindow.xaml 的交互逻辑
18     /// </summary>
19     public partial class MainWindow : Window
20     {
21         public MainWindow()
22         {
23             this.InitializeComponent();
24 
25             // 在此点下面插入创建对象所需的代码。
26             
27             
28         }
29         private RegexOptions GetRegexOptions()
30         {
31             RegexOptions temp = RegexOptions.None;
32             if (this.CheckBox_IgnoreCase.IsChecked == true)
33             {
34                 temp |= RegexOptions.IgnoreCase;
35             }
36             if (this.CheckBox_RightToLeft.IsChecked == true)
37             {
38                 temp |= RegexOptions.RightToLeft;
39             }
40             if (this.CheckBox_MultiLine.IsChecked == true)
41             {
42                 temp |= RegexOptions.Multiline;
43             }
44             if(this.CheckBox_SingleLine.IsChecked==true)
45             {
46                 temp|=RegexOptions.Singleline;
47             }
48             return  temp;
49 
50         }
51         private void Click_IsMatch(object sender, EventArgs s)
52         {
53             try
54             {
55                 RegexOptions SelectedRegexOptions = GetRegexOptions();
56                 //this.TextBox_RegexTemplate.Text = "\\w{1,}@\\w{1,}\\.";
57                 Regex a = new Regex(TextBox_RegexTemplate.Text, SelectedRegexOptions);
58                 if (a.IsMatch(TextBox_RegexTest.Text) == true)
59                 {
60                     
61                     TextBox_Result.Text = "正则表达式匹配";
62 
63                 }
64                 else
65                 {
66                     TextBox_Result.Text = "正则表达式不匹配";
67                 }
68             }
69             catch (ArgumentException e)
70             {
71                 TextBox_Result.Text = "出现一个错误" + e.Message.ToString();
72             }
73 
74 
75         }
76         private void Click_Clear(object sender, EventArgs e)
77         {
78             TextBox_RegexTemplate.Clear();
79             this.CheckBox_IgnoreCase.IsChecked = false;
80             this.CheckBox_MultiLine.IsChecked = false;
81             this.CheckBox_None.IsChecked = false;
82             this.CheckBox_RightToLeft.IsChecked = false;
83             this.CheckBox_SingleLine.IsChecked = false;
84             TextBox_RegexTest.Clear();
85             TextBox_Result.Clear();
86 
87         }
88 
89 
90     }
91 }

运行后,效果图

 

 

posted on 2012-07-12 15:46  凌波星子  阅读(166)  评论(0)    收藏  举报