透过代码学习正则表达式
package cn.zhangzq.reqexp;import java.util.regex.Matcher;import java.util.regex.Pattern;public class ReqExpTest {public static void main(String[] args) {// 一.简单认识正则表达式的概念/* * p("abc".matches("...")); p("a8729a".replaceAll("\\d", "-")); Pattern * p = Pattern.compile("{3}"); Matcher m = p.matcher("fgh"); * p(m.matches()); p("fgha".matches("{3}")); */// 二.初步认识. * + ?// .:1个字符, *:o个或多个字符,+:一个或多个字符 ?:once or not at all// X, once or not at all// X*? X, zero or more times// X+? X, one or more times// X{n}? X, exactly n times// X{n,}? X, at least n times// X{n,m}? X, at least n but not more than m times/* * p("a".matches(".")); p("aa".matches("aa")); p("b".matches("ba*")); * p("b".matches("ba*")); p("aaaa".matches("a+")); *// 注意这种情况永远返回TRUE p("".matches("a*")); p("aaaa".matches("a?")); * p("".matches("a?")); p("a".matches("a?")); * p("214523145234532".matches("\\d{3,100}")); * p("192.168.0.1".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")); * p("192".matches("")); */// 三.范围// a, b, or c (simple class)// [^abc] Any character except a, b, or c (negation)// a through z or A through Z, inclusive (range)// ] a through d, or m through p: (union)// ] d, e, or f (intersection)// ] a through z, except for b and c: (subtraction)// ] a through z, and not m through p: (subtraction)/* * p("a".matches("")); p("a".matches("[^abc]")); * p("A".matches("")); p("A".matches("|")); * p("A".matches("]")); p("R".matches("]")); */// 四.认识\s \w \d \// \d A digit: // \D A non-digit: [^0-9]// \s A whitespace character: [ \t\n\x0B\f\r]// \S A non-whitespace character: [^\s]// \w A word character: // \W A non-word character: [^\w]/* * p(" \n\r\t".matches("\\s{4}")); p(" ".matches("\\S")); * p("a_8".matches("\\w{3}")); * p("abc888&^%".matches("{1,3}\\d+[&^#%]+")); * p("\\".matches("\\\\")); */// 五.POSIX Style// \p{Lower} A lower-case alphabetic character: // \p{Upper} An upper-case alphabetic character:// \p{ASCII} All ASCII:[\x00-\x7F]// \p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]// \p{Digit} A decimal digit: // \p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}]// \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~// \p{Graph} A visible character: [\p{Alnum}\p{Punct}]// \p{Print} A printable character: [\p{Graph}\x20]// \p{Blank} A space or a tab: [ \t]// \p{Cntrl} A control character: [\x00-\x1F\x7F]// \p{XDigit} A hexadecimal digit: // \p{Space} A whitespace character: [ \t\n\x0B\f\r]// p("adfd".matches("\\p{Lower}{4,4}"));// 六.boundary// ^The beginning of a line// $ The end of a line// \b A word boundary// \B A non-word boundary// \A The beginning of the input// \G The end of the previous match// \Z The end of the input but for the final terminator, if any// \z The end of the input/* p("hello sir".matches("^h.*"));p("hello sir".matches(".*ir$")); p("hello sir".matches("^h{1,3}o\\b.*")); p("hellosir".matches("^h{1,3}o\\b.*")); //whilte linesp("*\n".matches("^[*]*\\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}{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}+");String s ="aaaa5bbbb68";Matcher m = p.matcher(s);if(m.find())p(m.start() +"-" + m.end());elsep("not match!"); }public static void p(Object o) {System.out.println(o);}}
页:
[1]