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". |
##(?<=pattern) | Reverse positive pre-check is similar to forward positive pre-check, but in the direction on the contrary. For example, "(?<=95|98|NT|2000)Windows" can match "Windows" in "2000Windows", but cannot match "Windows" in "3.1Windows". |
X { n }? | X, exactly n times |
##X { n ,}? | X, at least n times |
X { n , m}? | ##X, at least n times, but no more than m times |
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!