怎么描述一个字符串只含有一个字母[a-zA-Z]的情况?例如:
23423a
B48349
324c434
3498_ c-99

解决方案 »

  1.   


    void Main()
    {
    List<string>list=new List<string>
    {
      "23423a",
    "B48349",
    "324c434d",
    "3498_ c-99"
    };
    foreach(string str in list)
    {
      Console.WriteLine("{0}:\t{1}",str,Check(str));
    }
    /*
    23423a: True
    B48349: True
    324c434d: False
    3498_ c-99: True
    */
    }
    bool Check(string str)
    {
      return Regex.Matches(str,"[a-zA-Z]").Count ==1;
    }
      

  2.   

    字符串中可包含空格、回车吧,一次只能对一个字符串进行检测:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    namespace sxLdfang
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = @"23423G  678
    ";
                string pattern = @"^[^A-Za-z]*[A-Za-z][^A-Za-z]*$";
                MatchCollection mc = Regex.Matches(html, pattern);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }
                Console.ReadKey();
            }
        }
    }
    运行结果:
    23423G  678
      

  3.   

    不用那么麻烦的:
    re=/[a-zA-Z]{2,}/
    然后用,text测试一下。
      

  4.   

    bool Check(string str)
    {
      return Regex.Matches(str,"[a-zA-Z]").Count ==1;
    }