码迷,mamicode.com
首页 > Windows程序 > 详细

C#正则表达式基础

时间:2020-05-10 11:17:15      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:turn   regex   stat   write   i++   length   ons   false   ase   

namespace ---> System.Text.RegularExpressions.


        static void Main(string[] args)
        {
            // if (IsInputMatchesNumber())
            if (IsInputMatchesNumberByRegx())
            {
                Console.WriteLine("Input charectors are all numbers.");
            }
            else
            {
                Console.WriteLine("Input charectors are not pure numbers.");
            }
        }

        //Common way to judge whether a string is pure numbers or not
        static bool IsInputMatchesNumber()
        {
            Console.Write("Please input your password: ");
            string str = Console.ReadLine();
            bool isMatch = true;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] < ‘0‘ || str[i] > ‘9‘)
                {
                    isMatch = false;
                    break;
                }
            }
            return isMatch;
        }


        //Use regular expressions to judge, result is the same as above 
        static bool IsInputMatchesNumberByRegx()
        {
            Console.Write("Please input your password: ");
            string str = Console.ReadLine();
            //Regular expression always come with @
            // @  means "do not convert \ in string"
            // ^  means "start from"
            // $  means "end at"
            // *  means "has any"
            // \d means "number"
            string pattern = @"^\d*$";
            return Regex.IsMatch(str, pattern);
        }

C#正则表达式基础

标签:turn   regex   stat   write   i++   length   ons   false   ase   

原文地址:https://www.cnblogs.com/ezhar/p/12862266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!