SoFunction
Updated on 2025-03-07

Share the complete collection of commonly used C# rules

Commonly used regular expressions

Regular expression matching Chinese characters: [\u4e00-\u9fa5]


Comment: Matching Chinese is really a headache, it will be easier to do with this expression


Get date regular expression: \d{4}[year|\-|\.]\d{\1-\12}[month|\-|\.]\d{\1-\31}day?


Comment: Can be used to match most year, month and day information.


Match double-byte characters (including Chinese characters): [^\x00-\xff]


Comment: It can be used to calculate the length of a string (a double-byte character length meter 2, ASCII character meter 1)


Regular expression matching blank lines:\n\s*\r


Comment: Can be used to delete blank lines


Regular expression matching HTML tags: <(\S*?)[^>]*>.*?</>|<.*? />


Comment: The version circulating online is too bad, and the above one can only match the part, and it is still powerless to use complex nested markers.


Regular expression matching the beginning and end whitespace characters: ^\s*|\s*$


Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, page breaks, etc.), a very useful expression


Regular expression matching email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*


Comment: It is very practical when verifying the form


Regular expression matching URL: [a-zA-z]+://[^\s]*


Comment: The functions of the version circulating online are very limited, and the above can basically meet the needs


Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$


Comment: It is very practical when verifying the form


Match domestic phone number: \d{4}-\d{7}|\d{3}-\d{8}


Comment: Matching forms such as 0511 - 4405222 or 021 - 87888822


Match Tencent QQ number: [1-9][0-9]\{4,\}


Comment: Tencent QQ number starts at 1000 0


Match the Chinese postal code: [0-9]\d{5}(?!\d)


Comment: China's postal code is 6 digits


Match ID card: \d{17}[\d|X]|\d{15}


Comment: China's ID card is 15 or 18 digits


Match ip address: ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?).


Comment: It is useful when extracting IP addresses


Match specific numbers:


^[1-9]\d*$ //Match positive integer


^-[1-9]\d*$ //Match negative integers


^-?[1-9]\d*$ //Match integers


^[1-9]\d*|0$ //Match non-negative integers (positive integer + 0)


^-[1-9]\d*|0$ //Match non-positive integers (negative integer + 0)


^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //Match positive floating point number


^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //Match negative floating point numbers


^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //Match floating point numbers


^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //Match non-negative floating point numbers (positive floating point numbers + 0)


^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //Match non-positive floating point numbers (negative floating point numbers + 0)


Comment: It is useful when processing large amounts of data, please pay attention to corrections when applying it in detail.


Match a specific string:


^[A-Za-z]+$ //Match a string composed of 26 English letters


^[A-Z]+$ //Match a string composed of 26 English letters capitalizations


^[a-z]+$ //Match a string composed of 26 English letters lowercase


^[A-Za-z0-9]+$ //Match a string composed of numbers and 26 English letters


^\w+$ //Match a string composed of numbers, 26 English letters or underscores