directory

Regular expression

Java collection


Regular expression

Yesterday was a brief introduction to regular expressions (well, just one sentence). Personally, I think it’s amazing and fun, but I can’t directly express it clearly. Just a little bit of theoretical stuff.

1. A regular expression is a template for matching strings. In fact, any string can be used as a regular expression, such as “ABC”, which is also a regular expression, but only matches the “ABC” string.

2. Create regular expressions:

To create a regular expression is to create a special string. In this expression, there are many characters, predefined characters, boundary cards, special characters, and so on. Specific everybody by oneself baidu is good.

The number identifiers supported by regular expressions can be in the following modes:

(1) Greedy

(2) Reluctant (Reluctant)

(3) Possessive (Essive mode)

3. Use regular expressions:

Once you have defined regular expressions in your program, you can use Pattern and Matcher to use regular expressions.

The Pattern object is the compiled in-memory representation of the regular expression. Therefore, the regular expression string must be compiled into a Pattern object and then used to create the corresponding Matcher object. The state involved in performing the match remains in the Matcher object, and multiple Matcher objects can share the same Pattern object.

The find () and group () methods of the Matcher class allow you to extract specific substrings in turn from the target string.

Here’s an example:

 

The code shown above automatically looks for phone numbers in some text.

In the red box, you are creating Pattern and Matcher objects to find phone numbers, with quotes that start with 13 or 15, followed by a random Arabic numeral, followed by a random 8-digit string. The blue boxes below are the strings obtained using the find and group methods. The results are as follows:

It’s amazing, right? If the program goes further, it can extract hyperlink information from the Internet, then open other web pages according to the hyperlink, and then repeat the process on other web pages to achieve a simple web crawler.

There are many better operations, but I don’t want to write about internationalization and formatting. I don’t want to read chapter 7 now.

Java collection

The Java collection class is a particularly useful utility class for storing a variable number of objects and implementing commonly used data structures, such as stacks, queues, and so on. In addition, Java collections can be used to hold associative arrays with mappings. Java collections can be roughly divided into Set, List, Queue and Map. Set represents an unordered and unrepeatable collection. List denotes an ordered, repeated set; Map represents a collection of existing mappings. Java5 also added the Queue architecture collection, which represents a Queue collection implementation.

Overview of Java Collections

To hold an indefinite amount of data, as well as data that has mapped relationships (also known as associative arrays), Java provides collection classes. Collection classes are mainly responsible for storing and hosting other data, so collection classes are also called container classes.

Collection classes differ from arrays in that array elements can be values of primitive types or objects. Collections can only hold objects.

Java’s Collection classes are derived primarily from two interfaces: Collection and Map.

All Java collections are divided into three categories: Set collection, List collection, and Map collection.