【java】java.util.regex.Pattern和java.util.regex.Matcher简单示例

时间:2021-11-11 07:54:01
 package 正则;

 import java.util.regex.Matcher;
import java.util.regex.Pattern; public class Test_regex {
public static void main(String[] args) {
String str="a1b22c333d4444e_55_555f666666g7777777";
String regex="\\w*";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(str);
System.out.println(matcher.matches());//true
}
}

Matcher

 package 正则;

 import java.util.regex.Pattern;

 public class Test_regex {
public static void main(String[] args) {
String str="a1b22c333d4444e55555f666666g7777777";
String regex="\\d+";
Pattern pattern=Pattern.compile(regex);
for(String s:pattern.split(str))
System.out.print(s+" ");//a b c d e f g
}
}

Pattern