標籤:
今天學習了Regex,感覺亞曆山大,有很多地方沒動,一溜號就講完了,課後自己又研究了一下,總算明白了點,防止忘記,分享下今天學的,
構建Regex
\d 等於 [0-9] 數字
\D 等於 [^0-9] 非數字
\s 等於 [ \t\n\x0B\f\r] 空白字元
\S 等於 [^ \t\n\x0B\f\r] 非空白字元
\w 等於 [a-zA-Z_0-9] 數字或是英文字
\W 等於 [^a-zA-Z_0-9] 非數字與英文字
/d用法
1 Pattern pattern = Pattern.compile("//d")
2 Matcher matcher = pattern.matcher("1")
3 System.out.print(matcher.matches);
輸出匹配成功的字元
1 Pattern pattern = Pattern.compile("\\d+");2 Matcher matcher = pattern.matcher("234hui55651g23ghuihui6754hui");3 System.out.println("是否匹配成功"+matcher.matches());4 while (matcher.find()) {5 System.out.println("開始的位置"+matcher.start());6 System.out.println("匹配到的字元"+matcher.group());7 System.out.println("結束的位置"+matcher.end());8 }
分隔匹配成功的字元
Pattern pattern = Pattern.compile("a"); String str = "12321a123123asd123123ad213123a"; // System.out.println(pattern.split(str)); System.out.println(Arrays.toString(pattern.split(str)));
感覺自己寫的好亂,先不寫了,以後在寫把,勿噴
Android進階-Regex