1 The concept of regular expressions
Regular Expressions (English: Regular expression, often abbreviated as regex in code).
A regular expression is a string that is described using a single string, used to define a matching rule, and matches a series of strings that conform to a certain syntactic rule. In development, regular expressions are often used to retrieve and replace text that conforms to a rule.
2 matching rules for regular expressions
Referring to the help document, there is a rule definition in the pattern class that has a regular expression, which explicitly distinguishes the uppercase and lowercase letters in the regular expression. Let's learn the rules of grammar.
Syntax rules for regular expressions:
Character: X
Meaning: The character x is represented
For example: The match rule is "a", then the string content that needs to be matched is "a"
Character: \ \
Meaning: The backslash character ' \ ' is represented
For example: The match rule is "\ \" , then the string content that needs to be matched is "\"
Character: \ t
Meaning: Tab
For example: The match rule is "\ t", then the corresponding effect is to create a tab space
Character: \ n
Meaning: line break
For example: The match rule is "\ n", then the corresponding effect is a newline, the next line of the cursor in the original position
Character: \ r
Meaning: Carriage return character
For example: The matching rule is "\ r" , then the corresponding effect is the effect of carriage return, the cursor to the next line beginning
Character class: [ABC]
Meaning: The character A, B, or C is represented
For example: The match rule is "[ABC]" , then the match is a character a, or a character B, or a character C
Character class: [^ABC]
Meaning: Any character other than a, B, or C is represented
For example: The match rule is "[^ABC]", then the match is not a character a, or is not a character B, or is not a character C any one character
Character class: [A-za-z]
Meaning: Represents a to Z or A to Z, and the letters at both ends are included
For example: The match rule is "[a-za-z]", then you need to match an uppercase or lowercase letter
Character class: [0-9]
Meaning: represents 0 to 9 digits, both ends of the number are included
For example: The match rule is "[0-9]", then you need to match a number
Character class: [A-za-z_0-9]
Meaning: A representation of a letter or number or an underscore (that is, a word character)
For example: The match rule is "[a-za-z_0-9]", then you need to match a letter or a number or a glide line
Predefined character classes:.
Meaning: Represents any character
For example, if the match rule is ".", then a match is required for an arbitrary character. If you want to use it. , use the matching rule "\ \." To achieve
Predefined character classes: \d
Meaning: represents 0 to 9 digits, both ends of the number are included, equivalent to [0-9]
For example, if the match rule is "\d", then it needs to match a number
Predefined character classes: \w
Meaning: A representation of a letter or number or an underscore (that is, a word character), equivalent to [a-za-z_0-9]
For example: The match rule is "\w", then it needs to match a letter or a number or a glide line
Boundary match: ^
Meaning: Represents the beginning of a line
For example, if the matching rule is ^[abc][0-9]$ , then the content that needs to be matched starts at [ABC], which is equivalent to the left double quotation mark
Boundary Match: $
Meaning: Represents the end of a line
For example, if the matching rule is ^[abc][0-9]$ , then the match must end with [0-9], which is equivalent to the right double quotation mark
Boundary Matching device: \b
Meaning: The word boundary is represented
For example: The match rule is "\b[abc]\b" , which means that the right and left sides of the letter A or B or C need non-word characters ([a-za-z_0-9])
Quantity Word: X?
Meaning: X appears once or once or no
For example: The match rule is "A?", then the content that needs to be matched is a character a, or a does not have
Quantity Word: x*
Meaning: X appears 0 or more times
For example, if the match rule is "A *" , then the content that needs to be matched is multiple characters A, or a does not
Quantity Word: x+
Meaning: X appears one or more times
For example: The match rule is "A +", then the content that needs to be matched is more than one character a, or a
Quantity Word: X{n}
Meaning: The x appears exactly n times
For example: The match rule is "a{5}", then the content that needs to match is 5 characters a
Quantity Word: X{n,}
What it means: X appears at least n times
For example: The match rule is "a{5,}", then the content that needs to be matched is a minimum of 5 characters a
Quantity Word: X{n,m}
Meaning: X appears at least n times, but not more than M
For example: The match rule is "a{5,8}", then the content that needs to match is 5 characters A to 8 characters a
3 Regular Expression rule-matching exercises
Please write a string that satisfies the following matching rules:
Rule: "[0-9]{6,12}"
What the rule needs to match is a number with a length of 6 to a digit .
Example: Using the data "123456789" to match the result is true;
Use the data "12345" to match the result to false.
Rule: "1[34578][0-9]{9}"
What this rule needs to match is: one- Digit mobile phone number, 1th digit 1, 2nd Digit 3, 4, 5, 7 , One of 8, and 9 digits from 0 to 9 for any number in the back.
such as: Using the data "12345678901" to match the result is false;
The match result with the data "13312345678" is true.
Rule: "A*b"
The rule needs to match the following: There is a Bafter multiple a or 0 a; b must be the last character.
such as: Using the data "Aaaaab" to match the result is true;
Use the data "ABC" to match the result to false.
1.4 Common methods in string classes involving regular expressions
L public boolean matches(string regex)//Determine if a string matches a given rule
Example: Check the QQ number.
1: The requirement must be 5-15 digits
2:0 cannot begin
Code Demo:
String QQ = "604154942";
String regex = "[1-9][0-9]{4,14}";
boolean Flag2 = qq.matches (regex);
Example: Verifying a mobile phone number
1: Required for 11 digits
2: The 1th bit is 1, the 2nd bit is 3, 4, 5, 7, 8, and the next 9 bits are any number between 0 and 9.
Code Demo:
String phone = "18800022116";
String regex = "1[34578][0-9]{9}";
boolean flag = phone.matches (regex);
L Public string[] split(string regex)//split this string based on a matching rule for a given regular expression
Example: Splitting a number in a string
Code Demo:
String s = "18-22-40-65";
String regex = "-";
String[] result = S.split (regex);
Code Demo:
String s = "18 22 40 65";
String regex = "";
String[] result = S.split (regex);
L public string replaceall(string regex,string Replacement)//replace the string contents of the rule with the new string
Example: replacing numbers in text with *
Code Demo:
String s = "hello12345world6789012";
String regex = "[0-9]";
String result = S.replaceall (regex, "*");
1.5 Regular Expression Exercises
L MATCH the correct number
Matching rules:
Match positive integer: "\\d+"
Match positive decimals: "\\d+\\.\\d+"
Match negative integer: "-\\d+"
Match negative decimals: "-\\d+\\.\\d+"
Match a positive number that retains two decimal places: "\\d+\\.\\d{2}"
Match a positive number that retains 1-3 decimal places: "\\d+\\.\\d{1,3}"
L Match a legitimate mailbox
Matching rules:
"[A-za-z_0-9][email protected][a-za-z_0-9]+ (\\.[ a-za-z_0-9]+) + "
"\\[email protected]\\w+ (\\.\\w+) +"
L get each number in the IP address (192.168.1.100)
Matching rules:
”\\.”
The regular expression of Java learning