java--正则表达式的应用:读取文件,获取其中的电话号码
发布日期:2021-05-10 12:46:52 浏览次数:22 分类:精选文章

本文共 4904 字,大约阅读时间需要 16 分钟。

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

1������������������������

������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������?���������������������������������������������������������*, +, ?, {n}������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���.������������������������������������������������������������[\s\S]���������������������������

  • ������()���������������������������������������������������()������������������������(?:pattern)���������������������������������������������������������

  • ������(?=pattern)���������������(?<!pattern)������������������������������������������������������������������������������������������������������������������������������������

  • ���������{n}������������n������{n,}������������n������{n,m}������������n���m������


2���������������������

������������������������������13������15������18���������������������

  • 13x xxxx xxxx���15x xxxx xxxx���18x xxxx xxxx���������������������������������������������
  • ���������������������+86���������������86-���������������������������������������(86) 132-2214-4433���+86 1322214444���

3���������������������

������������������������������������������������������������������������������������

  • ������������������010���021���029���852���������������
  • ������������������0222���0333���������������������������
  • ������������1���4���������������������

4���������������

Java������������

import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public static void main(String[] args) {    String text = "13522158842;������������;test2;13000002222;8613111113313";        Pattern pattern = Pattern.compile("(?(?:\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))\\d*)");    Matcher matcher = pattern.matcher(text);        StringBuffer bf = new StringBuffer();    while (matcher.find()) {        bf.append(matcher.group()).append(",");    }        if (bf.length() > 0) {        bf.deleteCharAt(bf.length() - 1);    }    System.out.println(bf.toString());}

������������

  • ������������������(?:(\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))\\d*)���������������������������

    • \\d������������������������
    • (?:1[358]\\d{9})���������13���15���18������������������
    • (?:861[358]\\d{9})������������86������������������������������
    • \\d*���������������������������������������������������������
  • Matcher���Patterns���������������������������������������������������������������������������������StringBuffer������

  • ���������������������������������������������������������������������


5������������������������

Java������������

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.HashSet;import java.util.Set;public class ImportFile {    public static Set
getPhoneNumFromFile(String filePath) { Set
phoneSet = new HashSet<>(); 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) { CheckIfIsPhoneNumber.getPhoneNumFromStrIntoSet(lineTxt, phoneSet); } read.close(); } else { System.out.println("������������������������"); } } catch (Exception e) { System.out.println("������������������������"); e.printStackTrace(); } return phoneSet; } public static void main(String argv[]) { String filePath = "F:/three.txt"; Set
phoneSet = getPhoneNumFromFile(filePath); System.out.println("���������������" + phoneSet); }}

������������

  • File���FileInputStream������������������������
  • ReadingLines������������������������������
  • ������������������������������CheckIfIsPhoneNumber���������getPhoneNumFromStrIntoSet������������������������������������
  • Set���������������������������������������������������������������������������������������

6���������������

���������������three.txt������������������������������������

���������������[86132221, (86)13222144332, 86-13222144332, 32434343, (+86)13222144332, 13888888888]

������������������������������������������������������������������������������������������������������������������������������������

上一篇:Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE解决方法
下一篇:Excel两列数据比较,相同的保存在另外一列

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月07日 06时55分24秒