strongest collation of common regular expressions (quick reference manual)

1. the expression of the check digit

  1. digit:^[0-9]*$
  2. n-digit number:^\d{n}$
  3. numbers with at least n digits:^\d{n,}$
  4. numbers with m-n digits:^\d{m,n}$
  5. numbers beginning with zero and non-zero:^(0|[1-9][0-9]*)$
  6. numbers with up to two decimal places starting with nonzero:^([1-9][0-9]*)+(.[0-9]{1,2})?$
  7. positive or negative numbers with 1-2 decimal places:^(\-)?\d+(\.\d{1,2})?$
  8. positive, negative, and decimal:^(\-|\+)?\d+(\.\d+)?$
  9. a positive real number 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. nonzero positive integer:^[1-9]\d*$ or ^([1-9][0-9]*){1,3}$ or ^\+?[1-9][0-9]*$
  12. nonzero 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 number:^\d+(\.\d+)?$ or ^[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)$

second, the expression of the check character

  1. chinese character:^[\u4e00-\u9fa5]{0,}$
  2. english and digital:^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$
  3. all characters in length 3-20:^.{3,20}$
  4. a string of 26 english letters:^[A-Za-z]+$
  5. a string consisting of 26 uppercase english letters:^[A-Z]+$
  6. a string consisting of 26 lowercase english letters:^[a-z]+$
  7. a string consisting of numbers and 26 english letters:^[A-Za-z0-9]+$
  8. a string of numbers, 26 english letters, or underscores:^\w+$ or ^\w{3,20}
  9. chinese, english, numbers include underscores:^[\u4E00-\u9FA5A-Za-z0-9_]+$
  10. chinese, english, numbers but not underscores, etc.:^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
  11. you can enter characters that contain equal characters:^%&',;=?$\"[^%&',;=?$\x22]+
  12. it is forbidden to enter characters containing ~[^~\x22]+

other:

.*matches any character other than that.\n

/[\u4E00-\u9FA5]/ chinese character

/[\uFF00-\uFFFF]/ full-width symbol

/[\u0000-\u00FF]/ half-width symbol

third, special needs expressions

  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. InternetURL:[a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
  4. mobile 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-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
  6. domestic phone number (0511-4405222、021-87888822):\d{3}-\d{8}|\d{4}-\d{7}
  7. id number (15 digits, 18 digits):^\d{15}|\d{18}$
  8. short id number (number, letter x ending):^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
  9. whether the account number is legitimate (starts with a letter, allows 5-16 bytes, allows alphanumeric underscores):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
  10. password (starts with a letter, is between 6 and 18 in length, 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, cannot use special characters, and is between 8-10 in length):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
  12. date format:^\d{4}-\d{1,2}-\d{1,2}
  13. 12 months of the year (01~09 and 1~12):^(0?[1-9]|1[0-2])$
  14. 31 days of the month (01~09 and 1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$

input format of money:

1. there are four representations of money that we can accept: “10000.00” and “10,000.00”, and “10000” and “10,000” without “cents”:^[1-9][0-9]*$

2. this means any number that doesn’t start with a 0, however, it also means that a character “0” doesn’t pass, so we take the following form:^(0|[1-9][0-9]*)$

3. a 0 or a number that does not start with a 0. we can also allow the beginning to have a minus sign:^(0|-?[1-9][0-9]*)$

4. this represents a 0 or a number that may be negative and does not start with a 0. let the user start with a 0. remove the negative sign, because money can’t always be negative. below we’re going to add a section that illustrates the possible decimals:^[0-9]+(.[0-9]+)?$

5. it must be noted that there should be at least 1 digit after the decimal point, so “10.” it doesn’t work, but “10” and “10.2” do:^[0-9]+(.[0-9]{2})?$

6. in this way we stipulate that there must be two digits after the decimal point, if you think it is too harsh, you can do this:^[0-9]+(.[0-9]{1,2})?$

7.this allows the user to write only one decimal place. now it’s time to consider the comma in the number, we can do this:^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$

8.1 to 3 digits, followed by any comma + 3 digits, the comma becomes optional, not necessarily:^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$

(Search for the public account Java backend, reply to “2021”, and send you a Java interview question book)

note: this is the end result, don’t forget to use an alternative if you think an empty string is acceptable (strange, why?) finally, don’t forget to remove that backslash when using the function, where the general errors are+*

  1. xml file:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
  2. regular expressions for chinese characters:[\u4e00-\u9fa5]
  3. DOUBLE-BYTE CHARACTERS: (INCLUDING CHINESE CHARACTERS, CAN BE USED TO CALCULATE THE LENGTH OF A STRING (ONE DOUBLE-BYTE CHARACTER LENGTH COUNTS 2, ASCII CHARACTER COUNTS 1))[^\x00-\xff]
  4. regular expression for blank rows: (can be used to remove blank rows)\n\s*\r
  5. REGULAR EXPRESSION OF HTML TAGS: (THE VERSION CIRCULATING ON THE INTERNET IS TOO BAD, THE ABOVE ONE IS ONLY PARTIAL, AND THERE IS STILL NOTHING THAT CAN BE DONE ABOUT COMPLEX NESTED TAGS)<(\S*?)[^>]*>.*?</\1>|<.*? />
  6. regular expressions for end-to-end white-space characters: (very useful expressions that can be used to remove white-space characters at the beginning and end of a line (including spaces, tabs, page feeds, and so on)^\s*|\s*$or(^\s*)|(\s*$)
  7. TENCENT QQ: (TENCENT QQ NUMBER STARTS FROM 10000)[1-9][0-9]{4,}
  8. china zip code: (china zip code is 6 digits)[1-9]\d{5}(?!\d)
  9. IP ADDRESS: (USEFUL WHEN EXTRACTING IP ADDRESS)\d+\.\d+\.\d+\.\d+
  10. IP ADDRESS:((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))
  11. IP-v4 address: (Useful when extracting IP address)\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b
  12. Verify the IP-v6 address:(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
  13. subnet mask:((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))
  14. validation date: (date validation in “yyyy-mm-dd” format, with leap years considered.) )^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$
  15. extract annotations:<!--(.*?)-->
  16. TO FIND CSS PROPERTIES:^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}
  17. extract page hyperlinks:(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^" rel="external nofollow" ]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>
  18. extract web page images:\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)
  19. extract web page color codes:^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
  20. file extension validation:^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$
  21. JUDGE IE VERSION:^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$

Leave a Reply