使用正则判断汉字和捕获内容

 private void btn_GetCount_Click(object sender, EventArgs e)
        {
            int P_scalar = 0;//定义值类型变量并赋值为0
            Regex P_regex = //创建正则表达式对象,用于判断字符是否为汉字
                new Regex("^[\u4E00-\u9FA5]{0,}$");
            for (int i = 0; i < txt_str.Text.Length; i++)//遍历字符串中每一个字符
            {
                P_scalar = //如果检查的字符是汉字则计数器加1
                    P_regex.IsMatch(txt_str.Text[i].
                    ToString()) ? ++P_scalar : P_scalar;
            }
            txt_count.Text = P_scalar.ToString();//显示汉字数量
        }

 

假如从一段文字中选出以a开头的单词,并显示其前面和后面的5个字符可以进行如下操作:

 internal class Program
    {
         static void Main(string[] args)
        {
            Find2();
            Console.ReadKey();
        }

         static void WriteMatches(string text, MatchCollection matches)
         {
             Console.WriteLine("Original text was : \n\n"+text+"\n");
             Console.WriteLine("No. of matches:"+matches.Count);

             foreach (Match nextmatch in matches)
             {
                 int index = nextmatch.Index;
                 string result = nextmatch.ToString();
                 int charsBefore = (index < 5) ? index : 5;
                 int fromEnd = text.Length - index - result.Length;
                 int charsAfter = (fromEnd < 5) ? fromEnd : 5;
                 int charsToDisplay = charsBefore + charsAfter + result.Length;

                 Console.WriteLine("Index:{0}, \tString:: {1}, \t{2}",
                     index,result,text.Substring(index-charsBefore,charsToDisplay));
             }
         }

         static void Find1()
         {
             const string text = @"XML has made a major impact in almost every aspect of 
            software development. Designed as an open, extensible, self-describing 
            language, it has become the standard for data and document delivery on 
            the web. The panoply of XML-related technologies continues to develop 
            at breakneck speed, to enable validation, navigation, transformation, 
            linking, querying, description, and messaging of data.";

             const string pattern = @"\bn\S*\b";
             MatchCollection matches = Regex.Matches(text, pattern,
                 RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
             WriteMatches(text, matches);
          
         }

         static void Find2()
         {
             const string text = @"XML has made a major impact in almost every aspect of 
            software development. Designed as an open, extensible, self-describing 
            language, it has become the standard for data and document delivery on 
            the web. The panoply of XML-related technologies continues to develop 
            at breakneck speed, to enable validation, navigation, transformation, 
            linking, querying, description, and messaging of data.";
             const string pattern = @"\ba";
             MatchCollection matches = Regex.Matches(text, pattern,
                 RegexOptions.IgnoreCase);
             WriteMatches(text, matches);
         }
    }

结果如下图:

 

 

 

posted @ 2014-02-14 11:40  蚂蚁拉车  阅读(365)  评论(0编辑  收藏  举报