Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right
Article directory
Table of contents
First, what is a regular expression?
Second, regular expression symbols
Three, commonly used regular expression combinations
Fourth, the efficiency of regular expressions
Five, learn to write regular expressions
Match multiple characters edit
Six, generate regular expression plug-ins
foreword
Hello everyone, I love animal milk the most. Today I will introduce regular expressions to you. Follow my footsteps to have a look!
First, what is a regular expression?
Regular expression is a tool used to match and process text , it can use some specific symbols and syntax to describe the pattern of a class of strings. In computer science and various programming languages, regular expressions are widely used in text search, replacement, filtering, validation and so on. With regular expressions, we can easily match text that matches a certain pattern, such as matching email addresses, phone numbers, URLs, etc. In order to realize various text processing functions. Regular expressions can be used in a variety of programming languages, including but not limited to Java, Python, JavaScript, Perl, etc., and are widely used in computer programming, text editing, data processing, etc., and are a powerful and flexible tool.
Second, regular expression symbols
1. Metacharacters Metacharacters are characters with special meaning in regular expressions and they are used to match specific characters or character sets. Common metacharacters include: .: matches any character except newline. ^: matches the beginning of the string. $: Matches the end of the string. *: Matches the preceding character zero or more times. +: Matches the preceding character one or more times. ?: Matches the preceding character zero or one time. {n}: Matches the preceding character exactly n times. {n,}: Matches the preceding character at least n times. {n,m}: Match the preceding character at least n times, but not more than m times. []: Match any character in the character set. |: Indicates an OR relationship, matching any one of two or more expressions. (): Used for grouping, combining multiple elements into a whole. \: Used to escape metacharacters, making them lose their special meaning. 2. Escape characters Escape characters are used to convert metacharacters to ordinary characters or to convert ordinary characters to metacharacters. Common escape characters include: \d: Matches a numeric character. \D: Matches non-digit characters. \w: Match letters, numbers or underscore characters. \W: Matches characters that are not letters, numbers, or underscores. \s: Matches spaces, tabs, or newlines. \S: Matches a character that is not a space, tab, or newline. \b: Match a word boundary. \B: Matches a non-word boundary. \\: Matches the backslash character itself. It should be noted that when using regular expressions in Java the backslash character \ needs to be escaped as \\, otherwise a compilation error will occur. For example, to match a numeric character, you can use \d, but it needs to be written as \\d in Java.
Three, commonly used regular expression combinations
The following are commonly used
- Match email addresses:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
- Match mobile phone number:
^1[3-9]\d{9}$
- Match ID number (18 digits):
^\d{17}[\dXx]$
- Matching URLs:
^(http|https)://[a-zA-Z0-9./?%&=+-_]+$
- Match date (yyyy-mm-dd):
^\d{4}-\d{2}-\d{2}$
- Matching IP addresses:
^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$
- Match username (begins with a letter, allows alphanumeric underscores, length is 6-16 digits):
^[a-zA-Z][a-zA-Z0-9_]{5,15}$
The following basically covers all
- Match mobile phone number:
^1[3-9]\d{9}$
- Match ID number (15 or 18 digits):
^\d{15}(\d{2}[0-9xX])?$
- Matching Social Credit Code (18 digits):
^[0-9A-Z]{18}$
- Matching URLs:
^(http|https)://[a-zA-Z0-9./?%&=+-_]+$
- Match date (yyyy-mm-dd):
^\d{4}-\d{2}-\d{2}$
- Matching IP addresses:
^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$
- Match username (begins with a letter, allows alphanumeric underscores, length is 6-16 digits):
^[a-zA-Z][a-zA-Z0-9_]{5,15}$
- Matching password (8-16 characters in length, must contain letters and numbers):
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$
- Match Chinese characters:
^[\u4e00-\u9fa5]{0,}$
- Match postal codes:
^[1-9]\d{5}$
- Match MAC address:
^([0-9a-fA-F]{2}-){5}[0-9a-fA-F]{2}$
- Match QQ number:
^[1-9][0-9]{4,10}$
- Match the WeChat code (letters, numbers, underscores, and dashes are allowed, with a length of 6-20 digits):
^[a-zA-Z0-9_-]{6,20}$
- Matching integers:
^-?\d+$
- Match floats:
^-?\d+\.\d+$
- Match non-negative integers:
^\d+$
- Matches non-negative floating-point numbers:
^\d+\.\d+$
- Match positive integers:
^[1-9]\d*$
- Match positive floating point numbers:
^[1-9]\d*\.\d+|0\.\d*[1-9]\d*$
- Match non-positive integers:
^-[1-9]\d*|0$
- Match non-positive floating-point numbers:
^(-([1-9]\d*\.\d+|0\.\d*[1-9]\d*))|0\.{0,1}$
Fourth, the efficiency of regular expressions
If it is now required to verify whether a qq number is correct.
Rules: 1. Within 6 and 20 digits
2. 0 cannot be at the beginning
3. Must be all numbers.
First use the knowledge you have learned so far to complete the verification requirements and then experience regular expression verification.
Is it troublesome? It doesn't matter whether you think it's troublesome or not, anyway, I think it's troublesome
Why? Because I know regular expressions!
come on show
Five, learn to write regular expressions
Next, I will teach you to write regular expressions a little bit. If you don’t want to write, please skip to the plug-in. Of course, I recommend everyone to understand it. After all, there is no harm, right? So, come on !
Foreword:
matches
It is a method in Java that is used to judge whether a string conforms to the format of a regular expression. Its usage is as follows:String regex = "正则表达式"; String str = "待匹配字符串"; boolean isMatch = str.matches(regex);
matches a single character
/*
* 匹配单个字符
* */
// 只能是 a b c 中的一个
System.out.println("a".matches("[abc]"));// true
System.out.println("?".matches("[abc]"));// false
// 不能出现 a b c 中的一个
System.out.println("a".matches("[^abc]"));// true
System.out.println("?".matches("[^abc]"));// false
// a到z和A到Z(包括头尾的范围)
System.out.println("z".matches("[a-zA-z]")); // true
System.out.println("aa".matches("[a-zA-z]"));//false
System.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); //true
// [a-z&&[def]] a-z和def的交集。为:d,e,f
System.out.println("d".matches("[a-z&&[def]]")); //true
System.out.println("0".matches("[a-z&&[def]]")); //false
/*
\表示转义字符
两个\的理解方式:前面的\是一个转义字符,改变了后面\原本的含义,
把他变成一个普普通通的\而已。
*/
// . 表示任意一个字符
System.out.println("你".matches("..")); //false
System.out.println("你".matches(".")); //true
System.out.println("你我".matches(".."));//true
// \\d 表示任意的一个数字
// 简单来记:两个\表示一个\
System.out.println("a".matches("\\d")); // false
System.out.println("3".matches("\\d")); // true
// \\w 只能是一位单词字符[a-zA-Z_0-9]
System.out.println("z".matches("\\w")); // true
System.out.println("2".matches("\\w")); // true
System.out.println("你".matches("\\w"));//false
// 非单词字符 \\W
System.out.println("你".matches("\\W")); // true
match multiple characters
Six, generate regular expression plug-ins
look at the effect
The writing of the above regular expressions is very shallow for everyone, as long as you can understand it, most of the regular expressions can be directly searched
It can be used directly, but it is still useful to understand!!
Summarize
You only need to understand and be able to use regular expressions to solve problems. It is best to be able to understand regular expressions and write simple regular expressions. The next time I see regular expressions, I will probably tell you about crawling. Well, look forward to it!