正则表达式Regex

      

//简单认识正则表达式
p("abc".matches("..."));
p("a1234a".replaceAll("\\d","^"));
Pattern p = Pattern.compile("[a-z]{3}");
Matcher m = p.matcher("hll");
p(m.matches());

 

p("a".matches("."));
p("aa".matches("a*"));
p("aa".matches("aa"));
p("aaaa".matches("a*"));
p("aaaa".matches("a+"));
p("".matches("a?"));
p("".matches("a*"));

 

//大于12
p("1242343242342".matches("\\d{3,12}"));
p("1242343242342".matches("\\d{3,22}"));
p("192.168.255.2".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\{0,3}"));
p("192".matches("[0-2][0-9][0-9]"));

 

//认识\s \w \d \
p(" \n\r\t".matches("\\s(4)"));
p(" ".matches("\\S"));
p("a_8".matches("\\w(8)"));
p("ac4433221&*".matches("[a-z]{1,3}\\d+[&*^]+"));

 

//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.*"));
p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));//white lines

相关资料查询:

http://tool.oschina.net/apidocs/apidoc?api=jdk-zh    对应java.util.regex 下的Pattern

 

posted @ 2017-11-02 22:20  凯西_Casey  阅读(201)  评论(0编辑  收藏  举报