A method of extracting data using regular expressions in Java

What is a regular expression

Regular expressions are specifications that can be used for pattern matching and replacement, A regular expression is a text pattern composed of ordinary characters (such as characters a to Z) and special characters (metacharacters). It is used to describe one or more strings to be matched when finding the text body. The regular expression is used as a template to match a character pattern with the searched string.

Java uses regular expressions to extract data

Java regular expressions have a wide range of uses. Previously, it was used to cut a large 3M TXT text into multiple small texts. It is very concise to write in c# and the code is only more than 20 lines. I wrote it in Java today. Sure enough, Java is very wordy.

The code for segmenting files will not be pasted. It mainly focuses on how to use regular expressions to group large strings:

For example, there is now an endlist TXT text file, as follows:

The seven digits represent the first seven digits of the mobile phone number, and the following Chinese characters represent the place where the number belongs. Now I want to put these contents according to 130 131 132 Start with 130 txt 131. txt 132. txt..... In these documents.

Line 24 uses the regular expression "(\ \ D {3}) (\ \ D {4}, [\ u4e00 - \ u9fa5] * \ \ n)" the contents of each () are a group, the index starts from 1, and 0 represents the whole expression. Therefore, this expression is divided into two groups. The first group represents three numbers, and the second group represents four numbers plus multiple Chinese characters plus a newline character. The extraction is shown in lines 26-28.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

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
分享
二维码
< <上一篇
下一篇>>