標籤:
. any character
\d A digit 0-9
\D a non-digit [^0-9]
\s a whitespace character, 空白字元
\S a non-whitespace character
\w a word character [a-zA-Z0-9]
\W a non-word character
package DAO;import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestReg {public static void main(String[] args) throws Exception{/*System.out.println("abc".matches("...")); //true ctrl+shift+/ 注釋System.out.println("a898938593a".replaceAll("\\d","-")); //a---------aPattern p = Pattern.compile("[a-z]{3}"); //匹配具有3個字元的字串Matcher m = p.matcher("abc");System.out.println(m.matches()); //true*///初步認識 . * + ?, meta characters/*p("a".matches(".")); // true p("aa".matches("aa")); // true p("aaaa".matches("a*")); // true 0或者np("aaaa".matches("a+")); // true 1或者np("aaaa".matches("a?")); // false 0或者1p("".matches("a*")); // true 0或者np("".matches("a+")); // true 1或者np("".matches("a?")); // true 0或者1p("215454545454545".matches("\\d{3,100}")); // true 3~100個p("192.168.000.111".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")); //truep("192".matches("[0-2][0-9][0-9]")); //true*///範圍/*p("a".matches("[abc]")); // true []取出一個字元p("a".matches("[^abc]")); // false []取出一個字元p("A".matches("[a-zA-Z]")); // true []取出一個字元p("A".matches("[a-z]|[A-Z]")); // true []取出一個字元p("A".matches("[a-z[A-Z]]")); // true []取出一個字元p("R".matches("[A-Z&&|[RFG]]")); // true []取出一個字元*///認識 \s \w \d/*p(" \n\r\t".matches("\\s{4}")); //true 4個空白字元p(" ".matches("S")); //false S代表非空白字元p("a_8".matches("\\w{3}")); //truep("abc888&^%".matches("[a-z]{1,3}\\d+[&^$%]+")); //true 1-3個字母, 1或n個數字, [&^$%]裡任意1或n個p("\\".matches("\\\\")); //Regex裡 2個\\只代表一個逸出字元*///POSIX Style //p("a".matches("\\p{Lower}"));//boundary/*p("hello sir".matches("^h.*")); //truep("hello sir".matches(".*ir$")); //truep("hello sir".matches("^h[a-z]{1,3}o\\b.*")); //truep("hellosir".matches("^h[a-z]{1,3}o\\b.*")); //true \b是空格, 換行等單詞邊界*///white lines 空白行//p(" \n".matches("^[\\s&&[^\\n]]*\\n$")); //true 開頭是空白符, 而且不是斷行符號, 0或n次, 後面緊跟著分行符號//practice/*p("aaa 8888c".matches(".*\\d{4}.")); //true 0或n個字母, 4位元字, 然後有一個字母p("aaa 8888c".matches(".*\\b\\d{4}.")); //true 0或n個字母, 4位元字前面有個單詞邊界, 然後有一個字母p("aaa8888c".matches(".*\\d{4}.")); //true 0或n個字母, 4位元字, 然後有一個字母p("aaa8888c".matches(".*\\b\\d{4}.")); //false 0或n個字母, 4位元字前面沒有單詞邊界, 然後有一個字母*///email//p("[email protected]".matches("[\\w[.-]][email protected][\\w[.-]]+\\.[\\w]+")); //true//matches find lookingAt start end matches每次找整個串, find找字串, lookingAt每次都從頭找/*Pattern p = Pattern.compile("\\d{3,5}");String s = "123-34345-234-00";Matcher m = p.matcher(s);p(m.matches()); //把123-給吃了, 所以要reset以下. 否則, 如果不reset的話, 只有2個true 34345 234m.reset(); //為了把matches吃掉的字元恢複p(m.find()); //true 找一個匹配的字串 123p(m.start()+"-"+m.end()); //0-3p(m.find()); //true 找一個匹配的字串 34345p(m.start()+"-"+m.end()); //4-9p(m.find());//true找一個匹配的字串234p(m.start()+"-"+m.end()); //10-13p(m.find()); // false 00p(m.lookingAt()); //true 只從head開始找, 所以是123p(m.lookingAt()); //true 只從head開始找, 所以是123p(m.lookingAt()); //true 只從head開始找, 所以是123p(m.lookingAt()); //true 只從head開始找, 所以是123*///replacementPattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava");StringBuffer buf = new StringBuffer();int i=0;/*while(m.find()){p(m.group()); //列出所有java字樣的}*/while(m.find()){i++;if(i%2==0){m.appendReplacement(buf, "java"); //偶數都是java}else{m.appendReplacement(buf, "JAVA");//奇數都是JAVA}}m.appendTail(buf);p(buf); //列出所有java字樣的}public static void p(Object o){System.out.println(o);}}
JAVA基礎--Regex