Regular expressions
The regular expression defines the pattern of the string.
Regular expressions can be used to search, edit, or manipulate text.
Regular expressions are not limited to a single language, but have subtle differences in each language.
Grammar:
In Java, \ means: I want to insert a backslash for a regular expression, so the character after it has a special meaning.
String pattern = "(\\D*)(\\d+)(.*)";
Methods of the Matcher class
Index method
The index method provides useful index values that exactly indicate where the match is found in the input string:
Research methods
The research method is used to check the input string and return a Boolean value indicating whether the pattern is found:
Replacement method
The replacement method is the method of replacing the text in the input string:
Example:
public class regex {public static void main(String[] args) {checkQQ2("0123134");}public static void checkQQ2(String qq) {String reg = "[1-9][0-9]{4,14}";System.out.println(qq.matches(reg)?"合法qq":"非法qq");}}
Java Basics-Regular expressions