1. Match strings
Regular 1
//Regular 1 Regex r = new Regex("abc"); // Define a Regex object instance Match m = ("123abc456"); // Match in string if () { //match } //Regular 2 r = new Regex("(abc)*"); m = ("bcabcabc"); if () { //match }
Regular 2
//String matching string line = "ADDR=1234;NAME=ZHANG;PHONE=6789"; Regex reg = new Regex("NAME=(.+);"); //For example I want to extract the NAME value in the line Match match = (line); string value = [1].Value; ("valueThe value of:{0}", value);
2. String replacement
//String replacement //For example, I want to modify the NAME value in the following format record to WANG string line = "ADDR=1234;NAME=ZHANG;PHONE=6789"; Regex reg = new Regex("NAME=(.+);"); string modified = (line, "NAME=WANG;"); //The modified string is ADDR=1234;NAME=WANG;PHONE=6789
3. Match
//Match //The text contains "speed=30.3mph", and the speed value needs to be extracted, but the unit of speed may be metric or imperial, mph, km/h, m/s may be possible; in addition, there may be spaces before and after. string line = "lane=1;speed=30.3mph;acceleration=2.5mph/s"; Regex reg = new Regex(@"speed\s*=\s*([\d\.]+)\s*(mph|km/h|m/s)*"); Match match = (line); //Then in the returned result [1].Value will contain the value, and [2].Value will contain the unit. var value = [1].Value;//This is convenient for demonstration. Please do not use Chinese named variables in actual development. var unit = [2].Value; ("speed的value为:{0} speed的unit是:{1}", value, unit);
4. Extract the value of []
string pattern1 = @"(?is)(?<=\[)(.*)(?=\])"; string result1 = new Regex(pattern1).Match("sadff[xxx]sdfdsf").Value;
5. Extract the value of ()
string pattern2 = @"(?is)(?<=\()(.*)(?=\))"; string result2 = new Regex(pattern2).Match("sad(f)dsf").Value; string pattern3 = @"(?is)(?<=\{)(.*)(?=\})"; string result3 = new Regex(pattern3).Match("sadff[{xxx]sdfd}sf").Value;
6. Verify digital expressions
//number Regex reg = new Regex(@"^[0-9]*$"); //N digits Regex reg = new Regex(@"^\d{n}$"); //At least n digits Regex reg = new Regex(@"^\d{n,}$"); // m-n digit number Regex reg = new Regex(@"^\d{m,n}$"); //Numbers starting with zero and non-zero numbers Regex reg = new Regex(@"^(0|[1-9][0-9]*)$"); //Number of non-zero numbers with up to two decimal places Regex reg = new Regex(@"^([1-9][0-9]*)+(.[0-9]{1,2})?$"); // Positive or negative numbers with 1-2 decimal places Regex reg = new Regex(@"^(\-)?\d+(\.\d{1,2})?$"); // Positive, negative, and decimal Regex reg = new Regex(@"^(\-|\+)?\d+(\.\d+)?$"); // Positive real numbers with two decimal places Regex reg = new Regex(@"^[0-9]+(.[0-9]{2})?$"); //There are positive real numbers with 1 to 3 decimal places Regex reg = new Regex(@"^[0-9]+(.[0-9]{1,3})?$"); //Non-zero positive integer Regex reg = new Regex(@"^[1-9]\d*$ or ^([1-9][0-9]*){1,3}$ or ^\+?[1-9][0-9]*$"); //Non-zero negative integer Regex reg = new Regex(@"^\-[1-9][]0-9″*$ or ^-[1-9]\d*$"); //Non-negative integer Regex reg = new Regex(@"^\d+$ or ^[1-9]\d*|0$"); //Not positive integer Regex reg = new Regex(@"^-[1-9]\d*|0$ or ^((-\d+)|(0+))$"); //Non-negative floating point number Regex reg = new Regex(@"^\d+(\.\d+)?$ or ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$"); //Not positive floating point number Regex reg = new Regex(@"^((-\d+(\.\d+)?)|(0+(\.0+)?))$ or ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$"); //Positive floating point number Regex reg = new Regex(@"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"); //Negative floating point number Regex reg = new Regex(@"^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ or ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"); //Floating point number Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$");
7. Verify character expressions
//Chinese character Regex reg = new Regex(@"^[\u4e00-\u9fa5]{0,}$"); //English and numbers Regex reg = new Regex(@"^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$"); //All characters with length 3-20 Regex reg = new Regex(@"^.{3,20}$"); //A string composed of 26 English letters Regex reg = new Regex(@"^[A-Za-z]+$"); //A string composed of 26 capital English letters Regex reg = new Regex(@"^[A-Z]+$"); //A string composed of 26 lowercase English letters Regex reg = new Regex(@"^[a-z]+$"); //A string composed of numbers and 26 English letters Regex reg = new Regex(@"^[A-Za-z0-9]+$"); //A string composed of numbers, 26 English letters or underscores Regex reg = new Regex(@"^\w+$ or ^\w{3,20}$"); //Chinese, English, numbers include underline Regex reg = new Regex(@"^[\u4E00-\u9FA5A-Za-z0-9_]+$"); // Chinese, English, numbers but not underscores and other symbols Regex reg = new Regex(@"^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$"); //You can enter characters such as ^%&',;=?$\" and so on Regex reg = new Regex(@"[^%&',;=?$\x22]+"); //No entry of characters containing ~ is prohibited Regex reg = new Regex(@"[^~\x22]+");
8. Special needs expression
//Email address Regex reg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); //domain name Regex reg = new Regex(@"[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?"); //InternetURL Regex reg = new Regex(@"[a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"); //phone number Regex reg = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$"); //Phone Numbers ("XXX-XXXXXXXX", "XXXX-XXXXXXXXX", "XXX-XXXXXXXXXXX", "XXX-XXXXXXXXXXXX, "XXXXXXXXXXXXXX) Regex reg = new Regex(@"^($$\d{3,4}-)|\d{3.4}-)?\d{7,8}$"); //Domestic phone numbers (0511-4405222, 021-87888822) Regex reg = new Regex(@"\d{3}-\d{8}|\d{4}-\d{7}"); //Identity card number (15 digits, 18 digits) Regex reg = new Regex(@"^\d{15}|\d{18}$"); //Short ID number (end of number, letter x) Regex reg = new Regex(@"^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$"); //Is the account legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed) Regex reg = new Regex(@"^[a-zA-Z][a-zA-Z0-9_]{4,15}$"); //Password (starting with letters, lengths between 6 and 18, can only contain letters, numbers and underscores) Regex reg = new Regex(@"^[a-zA-Z]\w{5,17}$"); // Strong password (must contain a combination of upper and lower case letters and numbers, special characters cannot be used, the length is between 8-10) Regex reg = new Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"); //Date format Regex reg = new Regex(@"^\d{4}-\d{1,2}-\d{1,2}"); //The 12 months of a year (01-09 and 1-12) Regex reg = new Regex(@"^(0?[1-9]|1[0-2])$"); //31 days of a month (01-09 and 1-31) Regex reg = new Regex(@"^((0?[1-9])|((1|2)[0-9])|30|31)$"); //The input format of money: //We can accept four forms of money: "10000.00" and "10,000.00", and "10,000" and "10,000" without "score" Regex reg = new Regex(@"^[1-9][0-9]*$"); //This means any number that does not start with 0, but this also means that a character "0" does not pass, so we use the following form Regex reg = new Regex(@"^(0|[1-9][0-9]*)$"); //A 0 or a number that does not start with 0. We can also allow a negative sign to start with Regex reg = new Regex(@"^(0|-?[1-9][0-9]*)$"); //This means a 0 or a number that may be negative and not 0. Let the user start with 0. Remove the negative sign, because money cannot be negative. What we want to add below is the possible decimal part that explains the possible number Regex reg = new Regex(@"^[0-9]+(.[0-9]+)?$"); //It must be noted that there should be at least 1 digits after the decimal point, so "10." is not passed, but "10" and "10.2" are passed Regex reg = new Regex(@"^[0-9]+(.[0-9]{2})?$"); //In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can Regex reg = new Regex(@"^[0-9]+(.[0-9]{1,2})?$"); //This allows users to write only one decimal. Now we should consider the commas in the numbers, we can do this Regex reg = new Regex(@"^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$"); //1 to 3 numbers, followed by any comma + 3 numbers, comma becomes optional, not necessarily Regex reg = new Regex(@"^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$"); //Note: This is the final result, don’t forget that “+” can be replaced by “*”. If you think empty strings are acceptable (strange, why?) Finally, don't forget to remove the backslash when using functions. The general errors are here //xml file Regex reg = new Regex(@"^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$"); // Regular expression of Chinese characters Regex reg = new Regex(@"[\u4e00-\u9fa5]"); //Double byte characters Regex reg = new Regex(@"[^\x00-\xff] (Including Chinese characters,Can be used to calculate the length of a string(A double-byte character length meter2,ASCIICharacter meter1))"); // Regular expression of blank lines can be used to delete blank lines Regex reg = new Regex(@"\n\s*\r"); // Regular expression of HTML tag Regex reg = new Regex(@"<(\S*?)[^>]*>.*?</\1>|<.*? />");// (The version circulated online is too bad, and the above is only partial, and I still can't do anything about complex nested marks) // Regular expression of the beginning and end whitespace characters Regex reg = new Regex(@"^\s*|\s*$or(^\s*)|(\s*$)");// (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) //Tencent QQ number Regex reg = new Regex(@"[1-9][0-9]{4,}"); //(Tencent QQ number starts at 10000) //China Postal Code Regex reg = new Regex(@"[1-9]\d{5}(?!\d)");// (China's postal code is 6 digits) //IP address Regex reg = new Regex(@"\d+\.\d+\.\d+\.\d+");// (It is useful when extracting IP addresses) //IP address Regex reg = new Regex(@"((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))");
This is all about this article about .net regular expressions. I hope it will be helpful to everyone's learning and I hope everyone will support me more.