Home > Article > Backend Development > Detailed explanation of sample code of commonly used regular verification functions in C#
This article mainly introduces C#commonly used regular verificationfunction, This example analyzes C#’s related operating techniques for IP verification, price verification and positive integer verification. Friends who need it can refer to it.
This article describes the commonly used regular verification functions in C# and shares them with you for your reference. The details are as follows:
1. Ip address verification
/// <summary> /// Ip地址验证 /// </summary> public static bool CheckIp(string ip) { bool result = false; Regex ipReg = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); if (ipReg.IsMatch(ip)) { result = true; } return result; }
2. Price verification
/// <summary> /// 价格验证 /// </summary> /// <param name="priceStr"></param> /// <returns></returns> public bool CheckPrice(string priceStr) { bool result = false; Regex regex = new Regex(@"^\d+(\.\d{1,2})?$", RegexOptions.IgnoreCase); Match match = regex.Match(priceStr); if (match.Success) { result = true; } return result; }
3. Positive Integer validation
/// <summary> /// 正整数验证 /// </summary> public static bool CheckPositiveInteger(string numStr) { bool result = false; Regex regex = new Regex(@"^[1-9]\d*$", RegexOptions.IgnoreCase); Match match = regex.Match(numStr); if (match.Success) { result = true; } return result; }
The above is the detailed content of Detailed explanation of sample code of commonly used regular verification functions in C#. For more information, please follow other related articles on the PHP Chinese website!