Test.rar

  • PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2016-04-25 10:47
    上传日期
10分钟学会正则表达式,各种正则表达式的例子。
Test.rar
  • Test.java
    4.5KB
内容介绍
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { //简单认识正则表达式的概念 /*p("abc".matches("...")); p("a8729a".replaceAll("\\d", "-")); Pattern p = Pattern.compile("[a-z]{3}"); Matcher m = p.matcher("fgh"); p(m.matches()); p("fgha".matches("[a-z]{3}"));*/ //初步认识. * + ? //.代表一个字符,*代表0个或者多个,+代表1个或者多个,?代表1个或者0个 /*p("a".matches(".")); p("aa".matches("aa")); p("aaaa".matches("a*")); p("aaaa".matches("a+")); p("".matches("a*")); p("aaaa".matches("a?")); p("".matches("a?")); p("a".matches("a?")); p("214523145234532".matches("\\d{3,100}")); //1个数字,出现次数为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("a".matches("[abc]")); //匹配[]中abc的某一个即可 p("e".matches("[^abc]")); //除了[]中abc即可 p("A".matches("[a-zA-Z]")); //小写a到z或者大写A到Z即可 p("A".matches("[a-z]|[A-Z]")); //同上 p("A".matches("[a-z[A-Z]]")); //同上 p("R".matches("[A-Z&&[RFG]]")); //大写A到Z,并且还是REF之一 */ //认识\s \w \d \ //\s代表空白字符,\S代表不是空白字符,\w代表构成单词的字符(a-zA-Z_0-9),\W代表不能构成单词的字符,\d代表数字,\D代表不是数字 /*p(" \n\r\t".matches("\\s{4}")); p(" ".matches("\\S")); p("a_8".matches("\\w{3}")); p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+")); p("\\".matches("\\\\"));*/ //POSIX Style //p("a".matches("\\p{Lower}")); //boundary /*p("hello sir".matches("^h.*")); //开头是h,后边是0个或者多个字符 p("hello sir".matches(".*ir$")); //前边是0个或者多个字符,结尾是ir p("hello sir".matches("^h[a-z]{1,3}o\\b.*")); //\\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]+")); //matches find lookingAt /*Pattern p = Pattern.compile("\\d{3,5}"); String s = "123-34345-234-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 afasdfasdf"); 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); */ //group /* Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})"); String s = "123aa-34345bb-234cc-00"; Matcher m = p.matcher(s); while(m.find()) { p(m.group()); } */ //qulifiers /* Pattern p = Pattern.compile(".{3,10}+[0-9]"); String s = "aaaa5bbbb68"; 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 = "444a66b"; Matcher m = p.matcher(s); while(m.find()) { p(m.group()); }*/ //back refenrences /* Pattern p = Pattern.compile("(\\d(\\d))\\2"); String s = "122"; Matcher m = p.matcher(s); p(m.matches()); */ //flags的简写 //Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); //p("Java".matches("(?i)(java)")); /*p("3086073:".matches("\\d+:$|.+\\*$")); p("-3-".matches("-\\d-")); p("3086073: queue".matches("^\\d+:.+\\b")); p("2012-03-14 22:21:03".matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}$"));*/ Pattern p = Pattern.compile("-[A-Z]+-"); String s = "<132>Oct 22 07:37:40 192.168.46.219 u9901: 1905 Base LAG-WARNING-LagPortAddFailed-2003 [LAG]:"; Matcher m = p.matcher(s); while(m.find()) { p(m.group()); } } public static void p(Object o) { System.out.println(o); } }
评论
    相关推荐