Regular Expression, abbreviatedRegex,It is a powerful tool for matching and processing text. It can be used to search, replace or extract specific content in a string by defining a specific pattern.
Introduce namespace first
using ;
Intege(integer)
Must be a positive integer
// Must be a positive integer public static bool IsPositiveInteger(string txt) { Regex objReg = new Regex(@"^[1-9]\d*$"); return (txt); }
Positive integers and zeros
public static bool IsPositiveIntegerAndZero(string txt) { Regex objReg = new Regex(@"^[1-9]\d*|0$"); return (txt); }
Negative integers
public static bool IsNegativeInteger(string txt) { Regex objReg = new Regex(@"^-[1-9]\d*$"); return (txt); }
Both positive and negative
public static bool IsInteger(string txt) { Regex objReg = new Regex(@"^-?[1-9]\d*$"); return (txt); }
Decimal(decimal)
Positive number
public static bool IsPositiveDecimal(string txt) { Regex objReg = new Regex(@"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$"); return (txt); }
negative number
public static bool IsNegativeDecimal(string txt) { Regex objReg = new Regex(@"^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$"); return (txt); }
Both positive and negative
public static bool IsDecimal(string txt) { Regex objReg = new Regex(@"^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$"); return (txt); }
Other verifications
public static bool IsEmail(string txt) { Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); return (txt); }
ID card
public static bool IsIdentityCard(string txt) { Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"); return (txt); }
Email Code
public static bool IsPostalCode(string txt) { if ( != 6) return false; Regex objReg = new Regex(@"[1-9]\d{5}(?!\d)"); return (txt); }
This is the end of this article about C# data verification Regex. For more related C# data verification Regex content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!