This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21081|Reply: 0

[Source] C# is commonly used regular expressions

[Copy link]
Posted on 2/5/2015 9:41:50 AM | | |
Regular expressions are used in string processing, form validation, and other occasions, which are practical and efficient. Here are some commonly used expressions for emergencies.

Note: The following content comes from the Internet, and may not be applicable due to different regular expression libraries

Regular expression that matches HTML markup: <(\S*?)[^>]*>.*?</\1>| <.*? />
Comment: The version circulating on the Internet is too bad, and the above one can only match the part, and there is still nothing that can be done about complex nested markup

Regular expression that matches the first and last spaced characters: ^\s*|\s*$
Comment: A very useful expression that can be used to remove whitespace at the beginning and end of a line (including spaces, tabs, page breaks, etc.).

Regular expression to match the email address: \w+([-+.] \w+)*@\w+([-.] \w+)*\.\w+([-.] \w+)*
Comment: Useful for form validation

Regular expression for matching URL URL: [a-zA-z]+://[^\s]*
Comment: The version circulating on the Internet has very limited functions, and the above one can basically meet the needs

Matching account legitimacy (letter begins, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: Useful for form validation

Match domestic phone numbers: \d{3}-\d{8}|\d{4}-\d{7}
Comments: Matching forms such as 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts from 10,000

Match the Chinese postal code: [1-9]\d{5}(?! \d)
Comment: China's postal code is 6 digits

Matching ID:\d{15}|\d{18}
Commentary: Chinese ID cards are 15 or 18 digits

Matching IP address: \d+\.\d+\.\d+\.\d+
Comment: Useful when extracting IP addresses

Match specific numbers:
^[1-9]\d*$ // Matches positive integers
^-[1-9]\d*$ // Matches negative integers
^-? [1-9]\d*$ // matches integers
^[1-9]\d*|0$ // Matching non-negative integers (positive integers + 0)
^-[1-9]\d*|0$ //Matching non-positive integers (negative integers + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ // matches positive floating-point numbers
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //Matches the negative floating point number
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ // matches the floating-point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //Matching 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 number(negative floating-point number + 0)
Comment: Useful when dealing with large amounts of data, pay attention to correction when applying specific applications

Matching a specific string:
^[A-Za-z]+$ // matches a string consisting of 26 letters
^[A-Z]+$ // matches a string consisting of 26 uppercase letters
^[a-z]+$ // matches a string consisting of lowercase letters of 26 English letters
^[A-Za-z0-9]+$ // matches a string consisting of numbers and 26 letters
^\w+$ // Matches strings consisting of numbers, 26 letters, or underscores
Commentary: Some of the most basic and most commonly used expressions






Previous:IIS cannot add duplicate collection items of type add when configuring PHP
Next:Cisco routing command summary
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com