本文實例講述了java matches類,pattern類及matcher類用法。分享給大家供大家參考,具體如下:
pattern類
正則表達式常見規則
a:字符
x 字符 x。舉例:'a'表示字符a
\\ 反斜線字符。
\n 新行(換行)符 ('\u000a')
\r 回車符 ('\u000d')
b:字符類
[abc] a、b 或 c(簡單類),其中一個
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-za-z] a到 z 或 a到 z,兩頭的字母包括在內(范圍)
[0-9] 0到9的字符都包括
c:預定義字符類
. 任何字符。我的就是.字符本身,怎么表示呢? \.
\d 數字:[0-9]
\w 單詞字符:[a-za-z_0-9]
在正則表達式里面組成單詞的東西必須有這些東西組成
d:邊界匹配器
^ 行的開頭
$ 行的結尾
\b 單詞邊界
就是不是單詞字符的地方。
舉例:hello world?haha;xixi
e:greedy 數量詞
x? x,一次或一次也沒有
x* x,零次或多次
x+ x,一次或多次
x{n} x,恰好 n 次
x{n,} x,至少 n 次
x{n,m} x,至少 n 次,但是不超過 m 次
正則表達式的常見功能
a:判斷功能
string類的public boolean matches(string regex)
1
2
3
4
|
//定義手機號碼的規則 string regex = "1[38]\\d{9}" ; //調用功能,判斷即可 boolean flag = phone.matches(regex); |
1
2
3
4
|
//定義郵箱的規則 string regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+" ; //調用功能,判斷即可 boolean flag = email.matches(regex); |
b:分割功能
string類的public string[] split(string regex)
1
2
3
4
5
6
7
8
9
10
11
|
//定義一個年齡搜索范圍 string ages = "18-24" ; //定義規則 string regex = "-" ; //調用方法 string[] strarray = ages.split(regex); string s2 = "aa.bb.cc" ; string[] str2array = s2.split( "\\." ); //硬盤上的路徑,我們應該用\\替代\ string s4 = "e:\\javase\\day14\\avi" ; string[] str4array = s4.split( "\\\\" ); |
把字符串分割排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package cn.itcast_03; import java.util.arrays; /* * 我有如下一個字符串:"91 27 46 38 50" * 請寫代碼實現最終輸出結果是:"27 38 46 50 91" * * 分析: * a:定義一個字符串 * b:把字符串進行分割,得到一個字符串數組 * c:把字符串數組變換成int數組 * d:對int數組排序 * e:把排序后的int數組在組裝成一個字符串 * f:輸出字符串 */ public class regextest { public static void main(string[] args) { // 定義一個字符串 string s = "91 27 46 38 50" ; // 把字符串進行分割,得到一個字符串數組 string[] strarray = s.split( " " ); // 把字符串數組變換成int數組 int [] arr = new int [strarray.length]; for ( int x = 0 ; x < arr.length; x++) { arr[x] = integer.parseint(strarray[x]); } // 對int數組排序 arrays.sort(arr); // 把排序后的int數組在組裝成一個字符串 stringbuilder sb = new stringbuilder(); for ( int x = 0 ; x < arr.length; x++) { sb.append(arr[x]).append( " " ); } //轉化為字符串 string result = sb.tostring().trim(); //輸出字符串 system.out.println( "result:" +result); } } |
c:替換功能
string類的public string replaceall(string regex,string replacement)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package cn.itcast_04; /* * 替換功能 * string類的public string replaceall(string regex,string replacement) * 使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。 */ public class regexdemo { public static void main(string[] args) { // 定義一個字符串 string s = "helloqq12345worldkh622112345678java" ; // 直接把數字干掉 string regex = "\\d+" ; string ss = "" ; string result = s.replaceall(regex, ss); system.out.println(result); } } |
d:獲取功能
pattern和matcher
1
2
|
pattern p = pattern.compile( "a*b" ); matcher m = p.matcher( "aaaaab" ); |
find()
:查找存不存在
group()
:獲取剛才查找過的數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package cn.itcast_05; import java.util.regex.matcher; import java.util.regex.pattern; /* * 獲取功能 * pattern和matcher類的使用 * * 模式和匹配器的基本使用順序 */ public class regexdemo { public static void main(string[] args) { // 模式和匹配器的典型調用順序 // 把正則表達式編譯成模式對象 pattern p = pattern.compile( "a*b" ); // 通過模式對象得到匹配器對象,這個時候需要的是被匹配的字符串 matcher m = p.matcher( "aaaaab" ); // 調用匹配器對象的功能 boolean b = m.matches(); system.out.println(b); //這個是判斷功能,但是如果做判斷,這樣做就有點麻煩了,我們直接用字符串的方法做 string s = "aaaaab" ; string regex = "a*b" ; boolean bb = s.matches(regex); system.out.println(bb); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package cn.itcast_05; import java.util.regex.matcher; import java.util.regex.pattern; /* * 獲取功能: * 獲取下面這個字符串中由三個字符組成的單詞 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu? */ public class regexdemo2 { public static void main(string[] args) { // 定義字符串 string s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?" ; // 規則 string regex = "\\b\\w{3}\\b" ; // 把規則編譯成模式對象 pattern p = pattern.compile(regex); // 通過模式對象得到匹配器對象 matcher m = p.matcher(s); while (m.find()) { system.out.println(m.group()); } // 注意:一定要先find(),然后才能group() // illegalstateexception: no match found // string ss = m.group(); // system.out.println(ss); } } |
希望本文所述對大家java程序設計有所幫助。