正则表达式
1 package regex; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 /** 7 * 正则表达式 8 * @author XSW 9 * 10 */ 11 public class Test { 12 13 14 public static void main(String[] args) { 15 //正则表达式 16 /*System.out.println("asd".matches("...")); 17 18 p("a8888634a".replaceAll("\\d", "--")); 19 20 Pattern pattern = Pattern.compile("[a-z]{3}"); 21 22 Matcher matcher = pattern.matcher("cde"); 23 24 p(matcher.matches()); 25 26 p("add".matches("[a-z]{3}")); 27 */ 28 29 //认识一下 . * ? + 30 31 /* p("a".matches(".")); 32 p("aaa".matches("aaa")); 33 p("aaa".matches("a*")); 34 p("aaa".matches("a?")); 35 p("".matches("a?")); 36 p("aaa".matches("a+")); 37 p("".matches("a*")); 38 p("aaaaa".matches("a*")); 39 p("1234213434254356623523".matches("\\d{3,100}")); 40 p("192.136.22a".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")); 41 p("192".matches("[0-2][0-9][0-9]")); 42 */ 43 44 //范围 45 /*p("a".matches("[a-z]")); 46 p("a".matches("[^abc]")); 47 p("A".matches("[a-zA-Z]")); 48 p("A".matches("[a-z]|[A-Z]")); 49 p("A".matches("[a-z[A-Z]]")); 50 p("A".matches("[A-Z&&[ARF]]")); 51 p("R".matches("[A-Z&&[RFG]]")); 52 p("X".matches("[A-Z&&[ARF]]")); 53 */ 54 //认识 \s \w \d \ 55 /* p(" \t\n\f".matches("\\s{4}")); 56 p(" ".matches("\\S")); 57 p("asd_123".matches("\\w{7}")); 58 p("abc333&^$".matches("[a-z]{3}\\d+[&^$]+")); 59 p("\\".matches("\\\\")); 60 */ 61 62 //边界 63 /*p("hello world".matches("^h.*")); 64 p("hello world".matches("^.*ld$")); 65 p("hello world".matches("^h[ell]{1,3}o*")); 66 p("hello world".matches("^h[ell]{1,3}o [\\w]*")); 67 68 p("hello world".matches("^h[a-z]{1,3}o\\b.*")); 69 p("helloworld".matches("^h[a-z]{1,3}o\\b.*"));*/ 70 //空白行 71 /*p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));*/ 72 //Email 73 //p("asdfskfjgslakhjghjkj@hdjksfk.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+")); 74 //matches find lookingAt 75 /** 76 * matches 永远匹配整个字符串 77 * find 找子串 当和matches一起用时有影响,可以使用reset(),使其回复原始状态,在用find 78 * lookingAt 永远从头开始找 79 */ 80 81 /*Pattern pattern = Pattern.compile("\\d{3,5}"); 82 Matcher m= pattern.matcher("123-33453-234-00"); 83 84 p(m.matches()); 85 86 m.reset(); 87 88 p(m.find());*/ 89 /* 90 * start()是找到子串的起始位置, 91 * end()是找到的子串的末尾位置的下一个位置 92 */ 93 /*System.out.println(m.start()+"--"+m.end()); 94 p(m.find()); 95 System.out.println(m.start()+"--"+m.end()); 96 p(m.find()); 97 System.out.println(m.start()+"--"+m.end()); 98 p(m.find());*/ 99 /* 100 * 子串必须找到才能输出位置信息 101 * 102 */ 103 //System.out.println(m.start()+"--"+m.end()); 104 105 /*p(m.lookingAt()); 106 p(m.lookingAt()); 107 p(m.lookingAt()); 108 p(m.lookingAt());*/ 109 110 //replacement (替换) 111 112 /*Pattern pattern = Pattern.compile("java"); 113 Matcher m = pattern.matcher("java Java JaVA JAva ILoveJava you hateJava"); 114 115 while(m.find()){ 116 p(m.group()); 117 }*/ 118 119 /* Pattern pattern = Pattern.compile("java",Pattern.CASE_INSENSITIVE); 120 Matcher m = pattern.matcher("java Java JaVA JAva ILoveJava you hateJava"); 121 //替换成大写JAVA 122 p(m.replaceAll("JAVA"));*/ 123 //奇数位置上为大写,偶数上为小写 124 /*Pattern pattern = Pattern.compile("java",Pattern.CASE_INSENSITIVE); 125 Matcher m = pattern.matcher("java Java JaVA JAva ILoveJava you hateJava fdjsfkjasgfhlkjhgefs"); 126 StringBuffer buffer = new StringBuffer(); 127 int i = 0; 128 while(m.find()){ 129 i++; 130 if(i%2 == 0){ 131 m.appendReplacement(buffer, "java"); 132 }else{ 133 m.appendReplacement(buffer, "JAVA"); 134 } 135 } 136 m.appendTail(buffer); 137 p(buffer);*/ 138 139 //分组(group()) 140 /*Pattern pattern = Pattern.compile("\\d{3,5}[a-z]{2}"); 141 Matcher m = pattern.matcher("123as-14324ni-890hh-00"); 142 while(m.find()){ 143 p(m.group()); 144 }*/ 145 //进行分组,分组规则compile("(\\d{3,5})([a-z]{2})")在原来的分组规则上我加上了括号进行分组,(\\d{3,5})数字为一组 ([a-z]{2})字母为一组; 146 //怎么看是第几组,要看小括号的左半边数起compile("(\\d{3,5})([a-z]{2})")这个总共有三个左半边,分别为第0组数字字母组,第1组数字组,第2组字母组;什么够不填默认为0 147 Pattern pattern = Pattern.compile("(\\d{3,5})([a-z]{2})"); 148 Matcher m = pattern.matcher("123as-14324ni-890hh-00"); 149 while(m.find()){ 150 p(m.group(2)); 151 } 152 153 } 154 155 public static void p(Object object){ 156 157 System.out.println(object); 158 } 159 }
go big or go home -- facebook