C#正则表达式基础
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); }
相关推荐
flyingssky 2020-08-18
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03