實現功能:讀取文件,將其中的電話號碼存入一個Set返回。
方法介紹:
find():嘗試查找與該模式匹配的輸入序列的下一個子序列。
group():返回由以前匹配操作所匹配的輸入子序列。
1、從一個字符串中獲取出其中的電話號碼
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 從字符串中截取出電話號碼 * @author zcr * */ public class CheckIfIsPhoneNumber { /** * 獲得電話號碼的正則表達式:包括固定電話和移動電話 * 符合規則的號碼: * 1》、移動電話 * 86+‘-'+11位電話號碼 * 86+11位正常的電話號碼 * 11位正常電話號碼a * (+86) + 11位電話號碼 * (86) + 11位電話號碼 * 2》、固定電話 * 區號 + ‘-' + 固定電話 + ‘-' + 分機號 * 區號 + ‘-' + 固定電話 * 區號 + 固定電話 * @return 電話號碼的正則表達式 */ public static String isPhoneRegexp() { String regexp = "" ; //能滿足最長匹配,但無法完成國家區域號和電話號碼之間有空格的情況 String mobilePhoneRegexp = "(?:(\\(\\+?86\\))((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})|" + "(?:86-?((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})|" + "(?:((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})" ; // System.out.println("regexp = " + mobilePhoneRegexp); //固定電話正則表達式 String landlinePhoneRegexp = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" + "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)" ; regexp += "(?:" + mobilePhoneRegexp + "|" + landlinePhoneRegexp + ")" ; return regexp; } /** * 從dataStr中獲取出所有的電話號碼(固話和移動電話),將其放入Set * @param dataStr 待查找的字符串 * @param phoneSet dataStr中的電話號碼 */ public static void getPhoneNumFromStrIntoSet(String dataStr,Set<String> phoneSet) { //獲得固定電話和移動電話的正則表達式 String regexp = isPhoneRegexp(); System.out.println( "Regexp = " + regexp); Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(dataStr); //找與該模式匹配的輸入序列的下一個子序列 while (matcher.find()) { //獲取到之前查找到的字符串,并將其添加入set中 phoneSet.add(matcher.group()); } //System.out.println(phoneSet); } } |
2、讀取文件并調用電話號碼獲取
實現方式:根據文件路徑獲得文件后,一行行讀取,去獲取里面的電話號碼
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * 讀取文件操作 * * @author zcr * */ public class ImportFile { /** * 讀取文件,將文件中的電話號碼讀取出來,保存在Set中。 * @param filePath 文件的絕對路徑 * @return 文件中包含的電話號碼 */ public static Set<String> getPhoneNumFromFile(String filePath) { Set<String> phoneSet = new HashSet<String>(); try { String encoding = "UTF-8" ; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding); // 考慮到編碼格 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null ; while ((lineTxt = bufferedReader.readLine()) != null ) { //讀取文件中的一行,將其中的電話號碼添加到phoneSet中 CheckIfIsPhoneNumber.getPhoneNumFromStrIntoSet(lineTxt, phoneSet); } read.close(); } else { System.out.println( "找不到指定的文件" ); } } catch (Exception e) { System.out.println( "讀取文件內容出錯" ); e.printStackTrace(); } return phoneSet; } } |
3、測試
1
2
3
4
5
6
7
8
|
public static void main(String argv[]) { String filePath = "F:\\three.txt" ; Set<String> phoneSet = getPhoneNumFromFile(filePath); System.out.println( "電話集合:" + phoneSet); } |
文件中數據:
結果:
電話集合:[86132221, (86)13222144332, 86-13222144332, 32434343, (+86)13222144332, 13888888888]
以上就是整個應用的實現過程,希望大家通過這個案例,對java正則表達式使用更加熟練。