30 commonly used regular expressions, integers, floating point numbers, Chinese, English letters, mobile phone numbers, email addresses, dates, IPs, etc. (JavaScript, Java)

Preface

Regular expression (Regular Expression) is a powerful text pattern matching tool that uses specific grammar rules to perform pattern matching and text search on strings. Below, LZ has compiled some common regular expression patterns to help you better handle data validation. Without further ado, here are the commonly used regular expressions and their explanations:

regular expression

注意: In regular expressions, ^at the beginning and $at the end, we use /to wrap regular expressions to distinguish them from other content (such as modifiers). This is a common convention, especially in JavaScriptprogramming languages ​​such as , where /regular expressions wrapped in are a common notation. In Java, the regular expression syntax is 不需要wrapped with /.

  1. js example
const input = "Hello123!";
const pattern = /^0?$|^([1-9][0-9]*)?$/;
console.log(pattern.test(input)) //结果false
  1. java example
String input = "123";
String pattern = "^0?$|^([1-9][0-9]*)?$";
System.out.println(input.matches(pattern));//结果true

1. Positive integers contain zero:

/^0?$|^([1-9][0-9]*)?$/

Used to validate positive integers, including zero.

2. Integer:

/^-?\d+$/

Used to match an integer, which can be a positive or negative integer

3. Floating point numbers:

/^-?\d+(\.\d+)?$/

Used to match a floating point number, which can be a positive floating point number or a negative floating point number

4. English letters:

/^[a-zA-Z]+$/

Used to match a string consisting of English letters

5. English and numbers:

/^[a-zA-Z0-9]+$/

Used to match a string consisting of English letters and numbers

6. Chinese characters:

/^[\u4e00-\u9fa5]+$/

Used to match a string consisting of Chinese characters

7. Password (8-20 characters, including at least one uppercase letter, one lowercase letter and one number):

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/

The string used to match can only consist of uppercase and lowercase letters and numbers and must be between 8 and 20 characters in length.

8. Password (8-20 characters, including at least one uppercase letter, one lowercase letter, one number and one special character):

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+-={}[]:;"'<>,.?/]).{
    
    8,20}$/

The string used to match can only consist of uppercase and lowercase letters, numbers and special characters and must be between 8 and 20 characters in length.

9. 11-digit Chinese mobile phone number (simple):

/^1\d{10}$/

Simply verify the Chinese mobile phone number, which starts with 1 and is followed by 10 digits.

10. Mainland China mobile phone number (whether legal):

/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

Used to match a string that conforms to the format of mainland China mobile phone numbers

11. Fixed telephone number:

/^(?:\d{3}-)?\d{6,8}$/

Verify the landline number, the 3-digit area code -as a hyphen, and then the body of the phone number is at least 6 or 8 digits.

12. Mobile phone number (international format):

/^\+\d{1,3}-\d{1,14}$/

Format used to match phone numbers with an international area code, a plus sign character, the area code from 1 to 3 digits -as a hyphen, and then the body of the phone number from 1 to 14 digits.

13. Email (whether it is legal):

/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/

Regular expression to match valid email addresses, where:

  • ([a-zA-Z0-9_-])+ Indicates the mailbox name part, consisting of one or more letters, numbers, underscores, and dashes.
  • @Represents the at symbol in an email address.
  • ([a-zA-Z0-9_-])+Represents the domain name part, consisting of one or more letters, numbers, underscores, and dashes.
  • (\.[a-zA-Z0-9_-])+Represents the top-level domain name part, consisting of one or more letters, numbers, underscores, and dashes.
  • +Indicates matching the previous expression one or more times.

14. ID number (15 or 18 digits):

/^\d{15}|\d{18}$/

Used to verify the ID number, which can be 15 or 18 digits.

15. Postal code:

/^[1-9]\d{5}$/

Postal code format for matching six digits

16. Bank card number: (15 digits, 16 digits or 19 digits)

/^([1-9]{1})(\d{14}|\d{18}|\d{15})$/

Used to match numbers that are 15, 16 or 19 digits long

17. Username (5-20 characters, can include letters, numbers, and underscores):

/^[a-zA-Z0-9_]{5,20}$/

Used to match strings that are 5 to 20 characters long and contain only letters, numbers, or underscores

18. Time (hh:mm:ss format):

/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/

Time format used to match the 24-hour clock, in which the hour part can be from 00 to 23, and the minute and second parts must be two digits from 00 to 59.

19. Date (yyyy-MM-dd):

/^((((19|[2-9]\d)\d{2})-(0[13-9]|1[0-2])-(0[1-9]|[12]\d|30))|(((19|[2-9]\d)\d{2})-(0[1-9]|1[0-2])-31)|(((19|[2-9]\d)(0[48]|[2468][048]|[13579][26]))-02-29)|(((19|[2-9]\d)\d{2})-02-(0[1-9]|1\d|2[0-8]))|((((19|[2-9]\d)(0[13578]|1[02]))|((19|[2-9]\d)(0[469]|11)))-(30|31)))-\d{2}[0-9Xx]$/

Used to validate dates in yyyy-MM-dd format, taking into account leap years.

20. IP address:

/^((([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]))$/

Used to verify IP address, supports IPv4 format.

21. MAC address:

/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/

Used to verify the MAC address, supporting the standard format separated by colons or dashes.

22. License plate number:

/^[A-Z0-9\u4e00-\u9fa5]{1}[A-Z0-9\u4e00-\u9fa5]{5}[A-Z0-9\u4e00-\u9fa5挂学警港澳]{1}$/

Used to verify Chinese license plates, including ordinary cars, new energy vehicles and special license plates.

23. HTML tags:

/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/

Used to match simple HTML tags

24. File name (only letters, numbers, underscores, periods and hyphens allowed):

/^[a-zA-Z0-9_.-]+$/

Matches strings containing letters, numbers, underscores, dots, and dashes, where these characters can appear repeatedly

25. List of numbers and commas:

/^(\d+,)*\d+$/

Used to match a comma-separated list of integers, such as: "1,2,3,4,5": a comma-separated list of integers

26. Hexadecimal color code:

/^#[0-9A-Fa-f]{6}$/

Used to match hexadecimal color values ​​starting with the # symbol followed by a combination of six uppercase and lowercase letters and numbers

27. URL (legitimacy):

/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/

Used to verify the validity of URL addresses, supporting http and https.

28. Scientific notation of numbers (supports plus and minus signs):

/^[+-]?\d+(\.\d+)?(e[+-]?\d+)?$/

Used to match various forms of numbers, including integers, decimals, and numbers in scientific notation.

29. Hexadecimal characters:

/^[0-9A-Fa-f]+$/

Used to match whether a string consists entirely of hexadecimal digits ( 0-9, A-F, a-f).

30. UUID (Universal Unique Identifier):

/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/

Used to match a string that conforms to the UUID (Universally Unique Identifier) ​​standard format. UUID is an identifier whose format is similar to: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee, where a, b, c, d and e are all hexadecimal numbers ( 0-9, A-F, a-f)

Summarize

Regular expressions are powerful tools for processing and validating text data. They can be used in a variety of scenarios, including form validation, text search, and data cleaning. In programming and data processing, understanding and skillfully using regular expressions will effectively improve your development efficiency.

I hope the commonly used regular expressions provided in this article can be helpful to your development work!

Guess you like

Origin blog.csdn.net/Da_zhenzai/article/details/133033482