javaRegex的應用 java讀取檔案並擷取電話號碼_java

來源:互聯網
上載者:User

實現功能:讀取檔案,將其中的電話號碼存入一個Set返回。

方法介紹:

find():嘗試尋找與該模式比對的輸入序列的下一個子序列。

group():返回由以前匹配操作所匹配的輸入子序列。

1、從一個字串中擷取出其中的電話號碼

import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 從字串中截取出電話號碼 * @author zcr * */public class CheckIfIsPhoneNumber {    /**   * 獲得電話號碼的Regex:包括固定電話和行動電話   * 符合規則的號碼:   *   1》、行動電話   *     86+‘-'+11位電話號碼   *     86+11位正常的電話號碼   *     11位正常電話號碼a   *     (+86) + 11位電話號碼   *     (86) + 11位電話號碼   *   2》、固定電話   *     區號 + ‘-' + 固定電話 + ‘-' + 分機號   *     區號 + ‘-' + 固定電話    *     區號 + 固定電話   * @return  電話號碼的Regex   */  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);    //固定電話Regex        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)  {    //獲得固定電話和行動電話的Regex    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、讀取檔案並調用電話號碼擷取
實現方式:根據檔案路徑獲得檔案後,一行行讀取,去擷取裡面的電話號碼

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、測試

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]

以上就是整個應用的實現過程,希望大家通過這個案例,對javaRegex使用更加熟練。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.