import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
// 简单认识正则表达式的概念
// p("abc".matches("..."));
// p("a8729asj34fdfhdh".replaceAll("\\d", "-"));
// p("a8729asj34fdfhdh".replaceAll("\\D", "-"));
//
// Pattern p=Pattern.compile("[a-z]{3}");//编译好,速度快 Matcher
// m=p.matcher("fgh");//匹配后的结果 p(m.matches());
//
// p("fgh".matches("[a-z]{3}"));
// 初步认识 . * + ?
// p("a".matches("."));
// p("aa".matches("aa"));
// p("aaaa".matches("a*"));
// p("aaaa".matches("a?"));
// p("a".matches("a?"));
// p("".matches("a?"));
// p("aaaa".matches("a{4}"));
// p("aaaa".matches("a{2,}"));
// p("aaaa".matches("a{2,3}"));
// p("aaaa".matches("a{2,6}"));
// p("34567893456734567894356".matches("\\d{2,100}"));
// p("127.0.0.1".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("p".matches("[^abc]"));
// p("A".matches("[a-zA-Z]"));
// p("A".matches("[a-z]|[A-Z]"));
// p("A".matches("[a-z[A-Z]]"));
// p("R".matches("[A-Z&&[RFG]]"));
// 认识 \s \w \d \
// p(" \n\r\t".matches("\\s{4}"));
// p(" ".matches("\\S"));
// p("s".matches("\\S"));
// p("fhdsgufs346873".matches("\\w{2,}"));
// p("&_73".matches("\\w{2,}"));
// p("abc888$%#".matches("[a-z]{1,3}\\d+[#$%^&*]+"));
// p("\\".matches("\\\\"));
// 边界处理
// POSIX Style
// p("a".matches("\\p{Lower}"));
// boundary
// p("hello sir".matches("^h.*"));
// p("hello sir".matches(".*ir$"));
// p("hello sir".matches("^he.*ir$"));
// p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
// p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
// white lines
// p(" \n".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}."));
// Email
// p("Jack_Wong-2010@163.com".matches("[\\w[.-]]+@[\\w[.-]]+.[\\w]"));
// match find lookingAt
/*
* Pattern p = Pattern.compile("\\d{3,5}"); String s =
* "12434-5454-656-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.find());// 找子川
* p(m.start()+" - "+m.end()); p(m.find());// 找子川
* //p(m.start()+" - "+m.end());
*
* p(m.lookingAt()); p(m.lookingAt()); p(m.lookingAt());
* p(m.lookingAt());
*/
// replacement
/*
* Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
* Matcher m =
* p.matcher("java Java JaVa JAVa ILOVEJAVA you HATEjAVA fddsdgfbkjs");
* // p(m.replaceAll("JAVA")); StringBuffer buf = new StringBuffer();
* int i = 0; while (m.find()) { i++; if (i % 2 == 0) {
* m.appendReplacement(buf, "java"); } else { m.appendReplacement(buf,
* "JAVA"); } } m.appendTail(buf); p(buf);
*/
/*Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
String s = "123aa-34345bb-234-004cc";
Matcher m = p.matcher(s);
while(m.find()){
p(m.group(2));
}
*/
//qulifiers
/*Pattern p=Pattern.compile("(.{3,10}+)[0-9]");
String s="aaaa5bbbb6";
Matcher m=p.matcher(s);
if(m.find()){
p(m.start()+"-"+m.end());
}else{
p("not Match!");
}*/
//non-capturing groups
/*Pattern p=Pattern.compile(".{3}(?=a)");
String s="443a66b";
Matcher m=p.matcher(s);
if(m.find()){
p(m.group());
}*/
//back reference
/*Pattern p=Pattern.compile("(\\d(\\d))\\2");
String s="1212";
Matcher m=p.matcher(s);
if(m.find()){
p(m.group());
}*/
//flags的简写
p("Java".matches("(?i)(java)"));
}
public static void p(Object o) {
System.out.println(o);
}
}
正则表达式学习笔记
最新推荐文章于 2025-07-04 15:03:43 发布