Regular expression
Regular expressions are essentially a grammar rule for string operations. With it, we can more flexibly implement string matching, splitting, and replacing operations.
Regular mark
All the classes supported by regular expressions are defined in the java.util.regex package. The following two main classes are defined in this package:
1. Pattern class: mainly defines the expression object to be used
2. Matcher class: used to perform the matching operation of regular tags and specified content.
All the regular tags that can be used are defined in the java.util.regex.Pattern class description document. Commonly used tags are as follows:
1. Single character (number: 1)
- Character: indicates that it consists of one character;
- \\\: indicates the escape character "\";
- \ t: represents a "\ t" symbol;
- \ n: represents a "\ n" symbol;
2. Character set (quantity: 1)
- [abc]: indicates that it may be any one of character a, character b, and character c;
- [^ abc]: It is not any one of characters a, b, c;
- [az]: all lowercase letters;
- [a-zA-Z]: represents an arbitrary one-letter, not case sensitive;
- [0-9]: Represent any arbitrary digit;
3. Simplified character set expression (quantity: 1)
- .: Represents an arbitrary character;
- \ d: equivalent to "[0-9]", which belongs to simplified writing;
- \ D: equivalent to "[^ 0-9]", which belongs to simplified writing;
- \ s: represents any blank characters, for example: "\ t", "\ n";
- \ S: represents any non-white space character;
- \ w: equivalent to "[a-zA-Z_0-9]", which means that it consists of any letter, number, _;
- \ W: equivalent to "[a-zA-Z_0-9]", indicating that it is not composed of arbitrary letters, numbers, _;
4. Boundary assignment
- ^: Regular start;
- $: Regular end;
5. Quantity expression
- Regular ?: Indicates that this regular can appear 0 or 1 times;
- Regular +: indicates that this regular can appear once or more than once;
- Regular *: indicates that this regular can appear 0 times, 1 time or multiple times;
- Regular {n}: indicates that this regular appears exactly n times;
- Regular {n,}: indicates that this regular appears more than n times (including n times);
- Regular {n, m}: indicates that this regular appears n ~ m times;
6. Logic operation
- Regular 1 Regular 2: Regular 1 continues to judge regular 2 after the judgment is completed;
- Regular 1 | Regular 2: Regular 1 or regular 2 can be satisfied;
- (Regular): Take multiple regulars as a group, you can set the number of occurrences for this group separately.
Common methods of pattern
No. | Method name | Types of | description |
---|---|---|---|
1 | public static Pattern compile(String regex) | ordinary | Compile regular expressions |
2 | public String[] split(CharSequence input) | ordinary | Full data split operation |
3 | public String[] split(CharSequence input,int limit) | ordinary | Data split operation |
4 | public Matcher matcher(CharSequence input) | ordinary | Get Matcher class object |
Note: The Pattern class does not define a construction method, so if you want to obtain the Pattern class object, you must use the compile () method to compile the regular expression. At the same time, because the methods defined in the Pattern class receive CharSequence interface objects when receiving parameters, any subclass of the CharSequence interface can perform regular operations.
Example: Use the Pattern class to achieve string splitting
package Project.Study.PatternClass;
import java.util.Arrays;
import java.util.regex.Pattern;
public class Test1 {
public static void main(String[]args){
String str="hello3232world32!!!";
String regex="\\d+";
Pattern pattern=Pattern.compile(regex); //编译正则
String[] result =pattern.split(str); //拆分字符串
System.out.println(Arrays.toString(result));
}
}
//结果:
//[hello, world, !!!]
Common methods of the Matcher class
No. | Method name | Types of | description |
---|---|---|---|
1 | public boolean matches() | ordinary | Regular match |
2 | public String replaceAll(String replacement) | ordinary | Replace all |
3 | public String replaceFirst(String replacement) | ordinary | Replace the first |
Example: Implement string verification operation
package Project.Study.MatcherClass;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test1 {
public static void main(String[]args){
String str="100";
String regex="\\d+";
Pattern pattern=Pattern.compile(regex); //编译正则
Matcher matcher=pattern.matcher(str); //进行正则匹配
System.out.println(matcher.matches()); //匹配结果
}
}
//结果:
//true
String class support for regular
In fact, in actual development, the String class is used more. In most cases, the methods provided in the String class will be used to directly simplify the regular operation. Only in rare cases will the Pattern and Matcher classes be used to operate the regularity.
Five operation methods related to regularity in the String class
No. | Method name | Types of | description |
---|---|---|---|
1 | public boolean matches(String regex) | ordinary | Regular verification, use the specified string to determine whether it meets the given regular expression structure |
2 | public String replaceAll(String regex,String replacement) | ordinary | Replace all content that meets the regular mark with new content |
3 | public String replaceFirst(String regex,String replacement) | ordinary | Replace the first content that meets the regular mark with new content |
4 | public String[] split(String regex) | ordinary | Perform full string splitting according to the specified regular mark |
5 | public String[] split(String regex,int limit) | ordinary | Partially split the string according to the specified regular mark |
The above five methods include string replacement, splitting, and verification operations. Let ’s implement it
Example: implement string replacement
package Project.Study.Regex;
public class Test1 {
public static void main(String[]args){
String str="hel12l32o232121wQo@#rl32d!!!";//给出一组混乱的字符串
String regex="[^a-z]"; //编写正则,剔除所有不是小写字母的字符
System.out.println(str.replaceAll(regex,""));//字符串替换
}
}
//结果:
//helloworld
Example: String splitting
package Project.Study.Regex;
public class Test2 {
public static void main(String[]args){
String str="hello090world3241!!!";
String regex="\\d+"; //表示"[0-9]"一位以上的整数位,属于简化写法,因为是在字符串中定义的,所以用"\\"代替"\",属于转义字符操作
String[] result =str.split(regex);
for (String s : result) {
System.out.println(s); //输出分隔好的字符串
}
}
}
//结果:
//hello
//world
//!!!
Regular verification application
If the string is a number with a decimal point "." (Decimal point. "" Means any character in the regular), how should we convert it to floating point data?
Example: Convert character numbers to floating point data
package Project.Study.Regex;
public class Test3 {
public static void main(String[]args){
String str1="10.01";
String str2="10.10";
String regex="\\d+(\\.\\d+)?";
if(str1.matches(regex)){ //转型前要进行验证
System.out.println(Double.parseDouble(str1));
}
if(str2.matches(regex)){ //转型前要进行验证
System.out.println(Double.parseDouble(str2));
}
}
}
//结果:
//10.01
//10.1
Regular analysis in the
above program : "?" In the above program indicates that the decimal place may appear 0 or 1 times.
The matches () method is used in the above program for regular verification. Similarly, we can also use it to judge others, such as: IP address
Example:
package Project.Study.Regex;
public class Test4 {
public static void main(String[]args){
String str1="192.168.1.1";
String str2="192.1683.1.1";
String regex1="\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";//该正则中的"\\d{1,3}"表示1~3位数字
String regex2="(\\d{1,3}\\.){3}\\d{1,3}"; //简化正则操作
System.out.println(str1.matches(regex1));
System.out.println(str1.matches(regex2));
System.out.println(str2.matches(regex1));
System.out.println(str2.matches(regex2));
}
}
//结果:
//true
//true
//false
//false
Example: Determine whether the date format is correct
package Project.Study.Regex;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test5 {
public static void main(String[]args) throws ParseException {
String str="2013-8-15";
String regex="\\d{4}-\\d{1,2}-\\d{1,2}"; //定义验证正则
if(str.matches(regex)){ //符合规则
Date date=new SimpleDateFormat("yyyy-MM-dd").parse(str);
System.out.println(date);
}
}
}
//结果:
//Thu Aug 15 00:00:00 CST 2013