Java read file and get phone number function based on regular expression

This paper describes the functions of Java reading files and obtaining phone numbers based on regular expressions. Share with you for your reference, as follows:

1. Regular expression

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

Meaning analysis of some special regular expressions used:

Quantifier use

2. Mobile phone number

form

Country area number - Mobile Number

The mobile phone number format is relatively fixed, which is nothing more than 13X XXXX XXXX or 15x XXXX XXXX or 18x XXXX XXXX. The landline is more troublesome. For example, the long-distance area code becomes longer (3 or 4 digits) and the telephone number becomes longer (7 or 8 digits). Some still need to enter the extension number.

Generally, we can see that the solution to this complex problem is to separate the mobile phone number from the landline number. The landline number is divided into three sections, area code, telephone number + extension number. However, in order to make the form look fresh, the design gives a "universal" input box to input the user's phone number or mobile phone number.

Under the premise of such a demand, using complex regular expressions to solve the problem of verification is a fast solution.

First, get the easiest mobile phone number

Because the currently open section is 130-139150-159185-189180

If only the mobile phone (mobile phone) number is considered, the following method can be used

Only the mobile phone number can be matched. The following matching regular expression can be given:

(?:((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})

When we add the country region number (86) or (+ 86) or 86 - or directly 86, we can use the following regular expression:

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

Note: in order to match the longest phone number, it needs to be written in three sentences, and the relatively long ones need to be placed in the front, otherwise the later ones will not match after matching.

3. Landline number

form:

Country area code (+ 86, etc.) - area code - fixed telephone number - extension number

Three digit area code

010021-029852 (Hong Kong)

Because the three digit area code is an 8-digit telephone number, it can be written as

(010|021|022|023|024|025|026|027|028|029|852)\d{8}

Of course, it won't be so simple. Some people are used to the format of (010) xxxxxxxxx. We also need to support one and upgrade the above expression to

Look at the city with 4-digit area code

Here, it is simply judged that there is no area code of 0111 or 0222, and the telephone number is 7 or 8 digits.

Finally, the extension number (1-4 digits)

(? < extension > \ D? \ D {1,4})?

The above assembly is:

"(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)? ([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)| " + "(?:(86-?)? (0[0-9]{2,4})?) "

4. Coding implementation

Implementation function: read the file, store the phone number in a set and return.

Method introduction:

Find (): attempts to find the next subsequence of the input sequence that matches the pattern. Group (): returns the input subsequence matched by the previous matching operation.

① Get the phone number from a string

② , read the file and call the phone number to get

Implementation method: after obtaining the file according to the file path, read it line by line to obtain the phone number inside

③ , test

Data in file:

result:

Telephone collection: [86132221, (86) 13222144332,86-1322214433232434343, (+ 86) 13222144332138888888]

PS: here are two very convenient regular expression tools for your reference:

JavaScript regular expression online test tool: http://tools.jb51.net/regex/javascript

Regular expression online generation tool: http://tools.jb51.net/regex/create_reg

For more information about Java algorithms, readers who are interested can see the topics on this site: complete collection of Java regular expression skills, tutorial on Java data structure and algorithms, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>