Home >Java >javaTutorial >Detailed explanation of Java's function of reading files and obtaining phone numbers (based on regular expressions)

Detailed explanation of Java's function of reading files and obtaining phone numbers (based on regular expressions)

黄舟
黄舟Original
2017-09-06 14:16:571995browse

This article mainly introduces Java file reading and the function of obtaining phone numbers based on regular expressions. It analyzes in detail the relevant syntax of regular matching operations and the principles and implementation techniques of phone number matching in the form of examples. Friends who need it can Refer to the following

The example of this article describes the function of Java reading files and obtaining phone numbers based on regular expressions. Share it with everyone for your reference, the details are as follows:

1. Regular expression

Regular expression, also known as regular expression, regular expression Representation (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular expressions use a single string to describe and match a series of strings that match a certain syntax rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern.

Analysis of the meaning of some special constructed regular expressions used:

?

When this character immediately follows any of the other qualifiers (*,+,?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. Non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much of the searched string as possible. For example, for the string "oooo", "o+?" will match a single "o", while "o+" will match all "o"s.

. Dot

matches any single character except "\r\n". To match any character including "\r\n", use a pattern like "[\s\S]".

(pattern)

Match pattern and get this match. The obtained matches can be obtained from the generated Matches collection, using the SubMatches collection in VBScript and the $0...$9 attributes in JScript. To match parentheses characters, use "".

(?:pattern)

Matches the pattern but does not obtain the matching result, which means this is a non- Matches are obtained without storing them for later use. This is useful when combining parts of a pattern using the or character "(|)". For example, "industr(?:y|ies)" is a simpler expression than "industry|industries".

(?=pattern)

Positive positive lookup, at the beginning of any string matching pattern Match the search string. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, "Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1". Prefetching does not consume characters, that is, after a match occurs, the search for the next match begins immediately after the last match, rather than starting after the character containing the prefetch.

(?!pattern)

Forward negative lookup, starting at the beginning of any string that does not match pattern matches the search string. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, "Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1", but cannot match "Windows" in "Windows2000".

##(?c9f4370493e1e186cbe675bae6a92535\D?\d{1,4})?

以上拼装起来就是:

"(?:(\\(\\+?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})?)"

4、编码实现

实现功能:读取文件,将其中的电话号码存入一个Set返回。

方法介绍:

find():尝试查找与该模式匹配的输入序列的下一个子序列。
group():返回由以前匹配操作所匹配的输入子序列。

①、从一个字符串中获取出其中的电话号码


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);
 }
}

②、读取文件并调用电话号码获取

实现方式:根据文件路径获得文件后,一行行读取,去获取里面的电话号码


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;
 }
}

③、测试


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]

The above is the detailed content of Detailed explanation of Java's function of reading files and obtaining phone numbers (based on regular expressions). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn