1、简单认识正则表达式的概念
p(
"abc".matches(
"...")); //'.'代表字符
p( "a8729a".replaceAll( "\\d", "-"));// '\d'代表数字, 由于java中‘\’的转义,所以需要2个‘\’
Pattern p = Pattern.compile( "[a-z]{3}");//匹配a和z之间的3个字符, 先编译后效率更高
Matcher m = p.matcher( "fgh");
p(m.matches());
p( "fgha".matches( "[a-z]{3}"));
p( "a8729a".replaceAll( "\\d", "-"));// '\d'代表数字, 由于java中‘\’的转义,所以需要2个‘\’
Pattern p = Pattern.compile( "[a-z]{3}");//匹配a和z之间的3个字符, 先编译后效率更高
Matcher m = p.matcher( "fgh");
p(m.matches());
p( "fgha".matches( "[a-z]{3}"));
2、初步认识. * + ?
.:匹配1个字符
*:匹配任意次
+:匹配至少1次
?: 匹配0或1次
p(
"a".matches(
"."));
p( "aa".matches( "aa"));
p( "aaaa".matches( "a*"));
p( "aaaa".matches( "a+"));
p( "".matches("a*"));
p( "aaaa".matches( "a?")); //false
p( "".matches("a?"));
p( "a".matches( "a?"));
p( "214523145234532".matches( "\\d{3,100}"));
p( "192.168.0.aaa".matches( "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
p( "192".matches( "[0-2][0-9][0-9]")); //[]代表范围
p( "aa".matches( "aa"));
p( "aaaa".matches( "a*"));
p( "aaaa".matches( "a+"));
p( "".matches("a*"));
p( "aaaa".matches( "a?")); //false
p( "".matches("a?"));
p( "a".matches( "a?"));
p( "214523145234532".matches( "\\d{3,100}"));
p( "192.168.0.aaa".matches( "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
p( "192".matches( "[0-2][0-9][0-9]")); //[]代表范围
3、范围
p(
"a".matches(
"[abc]"));//取abc3个字符中的某一个
p( "a".matches( "[^abc]"));//false,取除了abc之外的字符
p( "A".matches( "[a-zA-Z]"));
p( "A".matches( "[a-z]|[A-Z]"));和上面的【a-zA-Z】一样
p( "A".matches( "[a-z[A-Z]]"));和上面的【a-zA-Z】一样
p( "R".matches( "[A-Z&&[RFG]]")); &&代表并且
p( "a".matches( "[^abc]"));//false,取除了abc之外的字符
p( "A".matches( "[a-zA-Z]"));
p( "A".matches( "[a-z]|[A-Z]"));和上面的【a-zA-Z】一样
p( "A".matches( "[a-z[A-Z]]"));和上面的【a-zA-Z】一样
p( "R".matches( "[A-Z&&[RFG]]")); &&代表并且
4、认识\s \w \d \
p(
" \n\r\t".matches(
"\\s{4}"));
p( " ".matches( "\\S")); //false
p( "a_8".matches( "\\w{3}"));
p( "abc888&^%".matches( "[a-z]{1,3}\\d+[&^#%]+"));
p( "\\".matches("\\\\"));
p( " ".matches( "\\S")); //false
p( "a_8".matches( "\\w{3}"));
p( "abc888&^%".matches( "[a-z]{1,3}\\d+[&^#%]+"));
p( "\\".matches("\\\\"));
5、
//POSIX Style
p( "a".matches( "\\p{Lower}"));
//boundary
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.*"));
//whilte 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( "asdfasdfsafsf@dsdfsdf.com".matches( "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
p( "a".matches( "\\p{Lower}"));
//boundary
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.*"));
//whilte 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}."));
p( "asdfasdfsafsf@dsdfsdf.com".matches( "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/307582,如需转载请自行联系原作者