How to interpret your regular expressions

Regular expressions are often used in our coding process. Correctly interpreting regular expressions is a very demanding task. Is there any way to use a machine to help us solve this problem? Here is a recommended website: The following is a set of screenshots of commonly used regular expression tests:

1. Digital Test

  1. number:^[0-9]*$
  2. n-digit number: ^\d{n}$such as:^\d{3}$
  3. At least n digits: ^\d{n,}$ such as:^\d{3,}$
  4. mn-digit numbers: ^\d{m,n}$, such as:^\d{3,10}$
  5. Numbers starting with zero and non-zero:^(0|[1-9][0-9]*)$
  6. Numbers with up to two decimal places and not starting with zero:^([1-9][0-9]*)+(.[0-9]{1,2})?$
  7. Positive or negative number with 1-2 decimal places:^(\-)?\d+(\.\d{1,2})?$
  8. Positive, negative, and decimal numbers:^(\-|\+)?\d+(\.\d+)?$
  9. Positive real numbers with two decimal places:^[0-9]+(.[0-9]{2})?$
  10. Positive real numbers with 1 to 3 decimal places:^[0-9]+(.[0-9]{1,3})?$
  11. Non-zero positive integer: ^[1-9]\d*$or ^([1-9][0-9]*){1,3}$or^\+?[1-9][0-9]*$
  12. A non-zero negative integer: ^\-[1-9][0-9]*$or^-[1-9]\d*$
  13. Non-negative integer: ^\d+$or^[1-9]\d*|0$
  14. Non-positive integer: ^(-[1-9]\d*|0)$or^((-\d+)|(0+))$
  15. Non-negative floating point numbers:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
  16. Non-positive floating point number: ^((-\d+(\.\d+)?)|(0+(\.0+)?))$or^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
  17. Positive floating point number: ^[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]*))$
  18. Negative floating point number: ^-([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]*)))$
  19. Floating point number: ^(-?\d+)(\.\d+)?$or^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

2. Expression of check characters

  1. Chinese character:^[\u4e00-\u9fa5]{0,}$
  2. English and numbers: ^[A-Za-z0-9]+$or^[A-Za-z0-9]{4,40}$
  3. All characters with a length of 3-20:^.{3,20}$
  4. A string consisting of 26 English letters:^[A-Za-z]+$
  5. A string of 26 uppercase English letters:^[A-Z]+$
  6. A string of 26 lowercase English letters:^[a-z]+$
  7. A string consisting of numbers and 26 English letters:^[A-Za-z0-9]+$
  8. A string consisting of numbers, 26 English letters, or underscores: ^\w+$or^\w{3,20}$
  9. Chinese, English, numbers including underscores:^[\u4E00-\u9FA5A-Za-z0-9_]+$
  10. Chinese, English, numbers but not including underscores and other symbols: ^[\u4E00-\u9FA5A-Za-z0-9_]+$or^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
  11. Contains ^%&',;=?$\"characters such as:[\^%&',;=?$\x22]+
  12. Does not contain characters containing ~:[^~\x22]+

3. Expression of specific requirements

  1. Email address:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
  2. domain name:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\/.?
  3. Internet URL: [a-zA-z]+:\/\/[^\s]*or^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$
  4. phone number:^(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}$
  5. Telephone numbers (“XXX-XXXXXXX”, “XXXX-XXXXXXXX”, “XXX-XXXXXXX”, “XXX-XXXXXXXX”, “XXXXXXX”, and “XXXXXXXX”):(\d{3,4}-)?\d{7,8}$
  6. Domestic telephone numbers (0511-4405222, 021-87888822):\d{3}-\d{8}|\d{4}-\d{7}
  7. ID number (15 or 18 digits):^(\d{15}|\d{18})$
  8. Short ID number (number, letter x at the end): ^([0-9]){7,18}(x|X)?$or^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
  9. Is the account legal (starts with a letter, 5-16 bytes allowed, alphanumeric characters and underscores allowed):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
  10. Password (starts with a letter, has a length between 6 and 18 characters, and can only contain letters, numbers, and underscores):^[a-zA-Z]\w{5,17}$
  11. Strong password (must contain a combination of uppercase and lowercase letters and numbers, no special characters, and a length between 8 and 10 characters):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
  12. Date Format:^\d{4}-\d{1,2}-\d{1,2}
  13. The 12 months of the year (01 to 09 and 1 to 12):^(0?[1-9]|1[0-2])$
  14. 31 days of a month (01 to 09 and 1 to 31):^((0?[1-9])|((1|2)[0-9])|30|31)$
  15. There are four forms of money that we can accept: “10000.00” and “10,000.00”, and “10000” and “10,000” without “cents”:^[0-9]+(\.[0-9]+)?\$
  16. xml file:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]$
  17. Regular expression for Chinese characters:[\u4e00-\u9fa5]
  18. Double-byte characters: [^\x00-\xff](<including Chinese characters, which can be used to calculate the length of a string (a double-byte character counts as 2, and an ASCII character counts as 1)>)
  19. Regular expression for blank lines: \n\s*\r(can be used to delete blank lines)
  20. Regular expressions for HTML tags:<(\S*?)[^>]*>.*?<\/\1>|<.*? \/>
  21. Regular expression for leading and trailing whitespace characters: ^\s*|\s*$or (^\s*)|(\s*$)(can be used to delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of a line, very useful expression)
  22. Tencent QQ number: [1-9][0-9]{4,}(Tencent QQ number starts from 10000)
  23. China Postal Code: [1-9]\d{5}(?!\d)(China Postal Code is 6 digits)
  24. IP address: \d+\.\d+\.\d+\.\d+(useful when extracting IP address)
  25. Extract annotations:<!--(.*?)-->

1 thought on “How to interpret your regular expressions”

Leave a Comment