regular expression
catalogue
1, What is a regular expression?
Purpose of regular expression:
How to know regular expressions
Java support for regular expressions
Usage example of Pattern class
Method usage example of String class
2. Predefined character classes
6. Backslash, escape and reference
1, What is a regular expression?
What is a regular expression?
Regular expression is composed of one character
String, which defines a pattern used to search for matching strings.
Purpose of regular expression:
Verify whether the string is legal. Find the string in the specified format, replace the string in the specified format, and extract the string in the specified format
Important statement
Regular expression is an independent pattern matching rule, which has its own format and semantic provisions. Nowadays, many languages support regular expressions to process text, such as Java, C#, Javascript, PHP, Perl, Python and so on. Different languages may support regular expressions in different ways and support different syntax details. Please use regular expressions according to specific languages.
How to know regular expressions
Compared with traditional programming, Java regular expression programming has the following advantages: simplifying code writing and improving processing efficiency. When judging Email, IP and other formats, using the traditional string comparison method may require dozens of lines of code, while using regular expression requires only a few lines to solve the problem. Disadvantages: you need to learn java regular expressions alone. The program written is not easy to understand and has poor readability
Java support for regular expressions
Use of regular expressions in Java: independent use: Java provides Java util. regex. Pattern class and Java util. regex. The matcher class supports the use of regular expressions in combination with the String class. The methods involved are: matches replaceAll split
Usage example of Pattern class
1. Sharing mode
//Compiles the given regular expression into a pattern. Pattern p = Pattern.compile("[a-z]{6,8}"); //Create a matcher that matches the given input Matcher m = p.matcher("asdasd");//6 ~ 8 letters //Try to match the entire region to the pattern. boolean b = m.matches(); System.out.println(b);
2. Independent mode
boolean flag=Pattern.matches("[a-z]{6,8}","asdasd"); System.out.println(flag);
Method usage example of String class
Several methods (matches, replaceAll, split) of the String class can be directly passed into regular expressions, as follows
public boolean matches(String regex) tells whether the string matches the given regular expression. This form is str The matches (regex) method produces exactly the same result as the expression Pattern. matches(regex, str) parameter regex - the regular expression to match this string result true, and only this string matches the given regular expression abnormal PatternSyntaxException - if the syntax of the regular expression is invalid
String pwd="asdfgh"; boolean flags=pwd.matches("[a-z]{6,8}"); System.out.println(flags);
Regular rules
1. Character class
A [] can only represent one character
package com.sk.test; /** * ClassName: Dome01 <br/> * Description: <br/> * date: 2021/12/28 9:11<br/> * * @author Zebra < br / > */ public class Dome01 { public static void main(String[] args) { //[abc] a, b or c (simple class) System.out.println("c".matches("[abc]")); System.out.println("55".matches("[235]")); System.out.println("55".matches("[1234566789][0123456789]")); //[^ abc] any character except a, b, or c (negative) System.out.println("a".matches("[^abc]")); //[a-zA-Z] a to Z or a to Z, both letters included (range) System.out.println("".matches("[a-z]"));//Lowercase letters System.out.println("".matches("[A-Z]"));//capital System.out.println("".matches("[0-9]"));//number System.out.println("".matches("[a-zA-Z]"));//Lowercase or uppercase letters System.out.println("".matches("[0-15-82]"));//0-1 5-8 2 System.out.println("".matches("[3-5][0-9]"));//30-59 //[a-d[m-p]] a to d or m to P: [a-dm-p] (Union) System.out.println("".matches("[0-9[a-z]]")); System.out.println("".matches("[0-9a-z]")); //[A-Z & & [def]] d, e, or f (intersection) System.out.println("".matches("[a-j&&[c-z]]")); //[A-Z & & [^ BC]] A to Z, except b and c: [ad-z] (minus) System.out.println("".matches("[a-j&&[^c-z]]")); //[A-Z & & [^ M-p]] A to Z, not m to p: [a-lq-z] (minus) System.out.println("".matches("[a-j&&[^c-z]]")); } }
2. Predefined character classes
package com.sk.test; /** * ClassName: Dome02 <br/> * Description: <br/> * date: 2021/12/28 10:02<br/> * * @author Zebra < br / > */ public class Dome02 { public static void main(String[] args) { // predefined character classes // . Any character (may or may not match the line terminator) System.out.println("".matches(".")); //\d number: [0-9] System.out.println("0".matches("\\d")); //\D non numeric: [^ 0-9] System.out.println("0".matches("\\D")); //\s white space character: [\ t\n\x0B\f\r] System.out.println("".matches("\\s")); //\s non white space character: [^ \ s] System.out.println("".matches("\\S")); //\w word character: [a-zA-Z_0-9] alphanumeric underscore System.out.println("".matches("\\w")); //\W non word character: [^ \ w] System.out.println("".matches("\\W")); } }
3. Boundary matcher
package com.sk.test; /** * ClassName: Dome03 <br/> * Description: <br/> * date: 2021/12/28 10:21<br/> * * @author Zebra < br / > */ public class Dome03 { public static void main(String[] args) { // Boundary matcher // ^Start of line System.out.println("".matches("^1\\d")); System.out.println("Zhang San ll".matches("^Zhang..")); // End of $line System.out.println("".matches("\\w\\w$")); System.out.println("".matches("..Abundant $")); //Zhang xfeng //\b word boundary //\B non word boundary //\A start of input //\G end of last match //\The end of the Z input, only for the last Terminator (if any) //\End of z input } }
4.Greedy quantifier
5.Logical operator
6. Backslash, escape and reference
Backslash, escape, and reference backslash characters ('\') are used to reference escape constructs, as defined in the above table, as well as other characters that will be interpreted as non escape constructs. Therefore, the expression \ \ matches a single backslash and \ {matches the left parenthesis. It is wrong to use backslashes before any alphabetic character that does not represent an escape construct; they are reserved for future extensions of the regular expression language. Backslashes can be used before non alphabetic characters, whether or not the character is part of a non escape construct.
package com.sk.test; /** * ClassName: Dome05 <br/> * Description: <br/> * date: 2021/12/28 14:23<br/> * * @author Zebra < br / > */ public class Dome05 { public static void main(String[] args) { // Logical operator // XY X followed by Y System.out.println("ab".matches("ab")); // X|Y X or Y System.out.println("male".matches("male|female")); // (X) X, as the content in the capture group (), will be treated as a whole //1-999 System.out.println("(\\d)|([1-9]\\d)|([1-9]\\d{2})"); // System.out.println("a:\\".matches("a:\\\\")); //verification. com suffix System.out.println("a.com".matches(".*\\.com")); //Verify( System.out.println("(".matches("\\(")); } }