Welcome my friend. Here is Standing's lifetime miracle......

Always Standing

Lifetime...
正则表达式小工具

Source Code Here:  https://files.cnblogs.com/Standing100/RegexToy.rar

以前听说正则表达式很牛,就跑去看了一下,但是没有实际的情景去应用总是看了忘、忘了看。最近工作需要分析一段相对复杂的文本,使我忽然想到了正则表达式,所以决定用正则表达式来实现文本的解析。
但是由于我用的不熟练所以不能一次写成,所以需要反复的实验,因此写了一个小工具来辅助正则表达式的开发,贴出来共享。灵感来源于www.codeproject.com  上的一篇帖子。
核心代码如下:

try
            
{

                Match m 
= RegexCheck(txtRegex.Text, txtText.Text);
            
                
if(null == m)
                
{
                    label1.BackColor 
= System.Drawing.Color.Orange;
                    lstInfo.Items.Add(
"Regular Expression parse error");
                    
return;
                }

            
                
if(string.Empty == txtRegex.Text)
                
{
                    
return;
                }


                
if(true == m.Success)
                
{
                    label1.BackColor 
= System.Drawing.Color.Green;
                    lstInfo.Items.Add(
"Match!");
                    lstInfo.Items.Add(
"Parsing");
                }

                
else
                
{
                    label1.BackColor 
= System.Drawing.Color.Red;
                    lstInfo.Items.Add(
"Not Match!");
                    
return;
                }


                
int matchCount = 0;

                
while(true == m.Success)
                
{
                    txtResult.AppendText(
"Match "+matchCount++.ToString()+System.Environment.NewLine);

                    
for(int i=0;i<m.Groups.Count;i++)
                    
{
                        Group g 
= m.Groups[i];
                        txtResult.AppendText(
" Group "+i.ToString()+" = "
                            
+g.Value+System.Environment.NewLine
                            
+"   Index = "+g.Index.ToString()+System.Environment.NewLine);
                    
                        
for(int j=0;j<g.Captures.Count;j++)
                        
{
                            Capture c 
= g.Captures[j];
                            txtResult.AppendText(
"   Capture "+j.ToString()+" = "
                                
+c.Value+System.Environment.NewLine
                                
+"     Index = "+c.Index.ToString()+System.Environment.NewLine);
                        }

                    }

                    m 
= m.NextMatch();
                }


                lstInfo.Items.Add(
"Done!");
            }

            
catch(Exception ex)
            
{
                lstInfo.Items.Add(
"Error:");
                lstInfo.Items.Add(ex.Message);
            }

posted on 2006-05-25 17:19  Standing  阅读(1417)  评论(5)    收藏  举报