import java.util.regex.Pattern;
public class Test2 {
p("abc".matches("..."));
p("a7856a".replaceAll("\\d", "-"));
p("a7856a".replaceAll("[0-9]", "-"));
Pattern p = Pattern.compile("\\d");
Matcher m = p.matcher("a7856a");
p(m.replaceAll("-"));
Pattern p1 = Pattern.compile("[a-z]{3}");
Matcher m1 = p1.matcher("fag");
p(m1.matches());
p("fagsa".matches("[a-z]{3}"));
p("a".matches("."));
p("aa".matches("aa"));
p("aaa".matches("a*"));
p("aaa".matches("a+"));
p("aaa".matches("a?"));
p("".matches("a*"));
p("".matches("a?"));
p("a".matches("a?"));
p("2134567889456415231".matches("\\d{3,100}"));
p("192.168.10.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
p("392".matches("[0-2][0-9][0-9]"));
p("a".matches("[abc]"));
p("a".matches("[^abc]"));
p("A".matches("[a-z]|[A-Z]"));
p("A".matches("[a-zA-Z]"));
p("A".matches("[a-z[A-Z]]"));
p("R".matches("[A-Z&&[RFG]]"));
p(" aaad".matches("\\s{4}"));
p(" ".matches("\\S"));
p("a_8".matches("\\w{3}"));
p("abc8855424524%^$".matches("[a-z]{1,3}\\d+[%^$]+"));
p("\\".matches("\\\\"));
p("a".matches("\\p{Lower}"));
p("a".matches("\\p{Upper}"));
p("hello sir".matches("^h.*"));
p("hello sir".matches(".*ir$"));
p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));//单词边界
p("hello sir".matches("^h[a-z]{1,3}o\\B.*"));//非单词边界
p("".matches("^[\\s&&[^\\n]]*\\n$"));//空白行
p("aaa 8888c".matches(".*\\d{4}."));
p("aaa 8888c".matches(".*\\b\\d{4}."));
p("aaa8888c".matches(".*\\d{4}."));
p("aaa8888c".matches(".*\\b\\d{4}."));
p("dlskfjsa.dlkjf@sda.jka.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
lookingAt
Pattern p3 = Pattern.compile("\\d{3,5}");
String s = "123-34345-234-00";
Matcher m3 = p3.matcher(s);
p(m3.matches());
p(m3.find());//找一个子串
p(m3.start() + "-" + m3.end());
p(m3.find());
p(m3.start() + "-" + m3.end());
p(m3.find());
p(m3.start() + "-" + m3.end());
p(m3.find());//false
p(m3.lookingAt());
p(m3.lookingAt());
p(m3.lookingAt());
p(m3.lookingAt());
Pattern p4 = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
Matcher m4 = p4.matcher("java Java JAVa JaVa IloveJAVA you hateJava dsfsdafsadfa");
p(m4.replaceAll("JAVA"));
StringBuffer buf = new StringBuffer();
int i=0;
while(m4.find()) {
i++;
if (i % 2 == 0) {
m4.appendReplacement(buf, "Java");
} else {
m4.appendReplacement(buf, "JAVA");
}
}
m4.appendTail(buf);
p(buf);
//group
Pattern p5 = Pattern.compile("(\\d{3,5})([a-z]{2})");
Matcher m5 = p5.matcher(s5);
while(m5.find()) {
//p(m5.group());
p(m5.group(1));
//p(m5.group(2));
}
Pattern p6 = Pattern.compile("(.{3,10}+)[0-9]");
String s6 = "aaaa5bbb69";
Matcher m6 = p6.matcher(s6);
if(m6.find()) {
p(m6.start() + "-" + m6.end());
} else {
p("not patch");
}
//non-capturing groups
Pattern p7 = Pattern.compile(".{3}(?=a)");
String s7 = "444a66b";
Matcher m7 = p7.matcher(s7);
while(m7.find()) {
p(m7.group());
}
//向上引用
Pattern p8 = Pattern.compile("(\\d\\d)\\1");
String s8 = "1234";
Matcher m8 = p8.matcher(s8);
p(m8.matches());
//flags的简写
//Pattern p9 = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
p("Java".matches("(?i)(java)"));//与上面的写法完全一样
}
public static void p(Object o) {
System.out.println(o);
}