public class Test {
public static void main(String[] args) {
// System.out.println("abc".matches("..."));
/* Pattern p = Pattern.compile("[a-z]{3}");
Matcher m = p.matcher("sfg");
System.out.println(m.matches());
System.out.println("\\");*/
/*System.out.println("\\".matches("\\\\"));
System.out.println(" \n\r\t".matches("\\s{4}"));
System.out.println(" ".matches("\\S"));.0
System.out.println("a_6".matches("\\w{3}"));
System.out.println("abc888&^%".matches("[a-z]+\\d+[&^%]+"));*/
//匹配汉字
/*System.out.println("孙悟空".matches("^[\\u4e00-\\u9fa5]{0,}$"));
//white lines
System.out.println(" \n".matches("^[\\s&&[^\\n]]*\\n$"));*/
//匹配email地址1125334959@qq.com menglingshaui@outlook.com menglingshaui@163.com
/*String email = "[\\w-]+@[\\w[-.]]+\\.[\\w]+";
String s = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
String m = ".";
p("kefu@youkuaiyun.com".matches(email));*/
//matches find lookingAt
/* Pattern p = Pattern.compile("\\d{3,5}");
String s ="123-45678-654-00";
Matcher m = p.matcher(s);*/
//p(m.matches());
//m.reset();
/*p(m.find());
p(m.start() + "-" + m.end());
p(m.find());
p(m.start() + "-" + m.end());*/
//p(m.lookingAt());
//p(m.lookingAt());
/* Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava");
StringBuffer buf = new StringBuffer();
p(buf);
int index = 0;
while(m.find()) {
// p(m.group());
index ++;
if(index %2 == 0) {
m.appendReplacement(buf,"JAVA");
} else {
m.appendReplacement(buf,"java");
}
}
p(buf);*/
//group
/* Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
String s = "123aa-34345bb-234cc-0002cc";
Matcher m = p.matcher(s);
while(m.find()) {
p(m.group());
}*/
//non-capturing groups
Pattern p = null;
//p = Pattern.compile(".{3}(?=a)");
//p = Pattern.compile(".{3}(?!a)");
//p = Pattern.compile("(?=a).{3}");
/* p = Pattern.compile("(?!a).{3}");
Matcher m = p.matcher("444a55j");
while(m.find()) {
p(m.group());
}*/
//back refenrences
/* p = Pattern.compile("(\\d(\\d))\\1");
Matcher m = p.matcher("1212");
System.out.println(m.find());*/
//flags 的简写
// p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
p = Pattern.compile("(?i)(java)");
Matcher m = p.matcher("JAVA java");
while(m.find()) {
p(m.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
Java_Regex
最新推荐文章于 2024-07-19 15:39:36 发布