C#-正则基础 [0-9]|[a-z]|[A-Z] 匹配数字或字母

慈心积善融学习,技术愿为有情学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。


  •    .NET Framework : 4.6.1
  •                           ide : visual studio 2017 community
  •                             os : win7 x86_64
  •            type setting : markdown
  •                         blog : https://blog.csdn.net/yushaopu
  •                     github : https://github.com/GratefulHeartCoder

code

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp
{

    class Program
    {
        static void Main(string[] args)
        {
            // " [0-9]|[a-z]|[A-Z] "
            // 正则表达式前后有空格的话,会出现错误的结果。
            string regularExpression = "[0-9]|[a-z]|[A-Z]";
            Regex rg = new Regex(regularExpression);

            string contents = "012abcEF!@#¥%……&*()——+";
            for (int i = 0; i < contents.Length; i++)
            {
                if (rg.IsMatch(contents[i].ToString()))
                {
                    Console.WriteLine(contents[i] + "成功");
                }
                else
                {
                    Console.WriteLine(contents[i] + "不成功");

                }
            }

            Console.ReadKey();
        }
    }
}

result

0成功
1成功
2成功
a成功
b成功
c成功
E成功
F成功
!不成功
@不成功
#不成功
¥不成功
%不成功
…不成功
…不成功
&不成功
*不成功
(不成功
)不成功
—不成功
—不成功
+不成功

resource


感恩曾经帮助过 心少朴 的人。
C#优秀,值得学习。正则表达式要谨慎学习,小心使用(个人建议)。
Console,WinForm,WPF,ASP.NET,Azure WebJob,WCF,Unity3d可以适当地了解一下。
注:此文是自学笔记所生,质量中下等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。

猜你喜欢

转载自blog.csdn.net/yushaopu/article/details/81835584