"Crazy Java Lectures" 9

table of Contents

Regular expression

Java collection


Regular expression

    Yesterday, I briefly introduced regular expressions (well, just one sentence). Personally, I think this thing is quite amazing and fun, but it is not easy for me to express it clearly. Just talk about some theoretical things.

1. Regular expression is a template used to match strings. In fact, any string can be used as a regular expression, such as "abc", which is also a regular expression, but it can only match the string "abc".

2. Create a regular expression:

    To create a regular expression is to create a special string. In this expression, there are many characters, predefined characters, boundary matching characters, special characters, etc. Specifically, you can Baidu by yourself.

    The number identifier supported by regular expressions has the following modes:

    (1) Greedy (greedy mode)

    (2) Reluctant (reluctant mode)

    (3) Possessive (possession mode)

3. Use regular expressions:

    Once the regular expression is defined in the program, you can use Pattern and Matcher to use the regular expression.

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

    Through the find () and group () methods of the Matcher class, specific substrings can be retrieved from the target string in turn.

Give a chestnut:

 

image

The code shown above is to automatically find the phone number in some text.

    The red box is creating Pattern and Matcher objects. The purpose is to find the phone number. The meaning in the quotation marks is: beginning with 13 or 15, followed by an Arabic numeral, and then a string of 8 digits. The blue box below is to obtain the required string through the find and group methods. The results are as follows:

image

    It's still amazing, right! If the program goes a step further, you can extract hyperlink information from the Internet, open other web pages based on the hyperlink, and repeat this process on other web pages to implement a simple web crawler.

    In fact, there are many better operations, but I don’t want to write about what internationalization and formatting are behind, so I don’t want to read it now~ Chapter 7 is over~~ Oh yeah! ! !

Java collection

    The java collection class is a particularly useful tool class, which can be used to store an appropriate amount of objects, and can implement commonly used data structures, such as stacks, queues, etc. In addition, Java collections can also be used to store associative arrays with mapping relationships. Java collections can be roughly divided into four systems: Set, List, Queue and Map. Set represents an unordered, non-repeatable collection; List represents an ordered and repeated collection; and Map represents a collection with existing mapping relationships; Java 5 adds The Queue system collection represents a kind of queue collection implementation.

Overview of Java collections

    In order to save an uncertain amount of data, as well as to save data with a mapping relationship (also known as associative array), Java provides a collection class. The collection class is mainly responsible for storing and holding other data, so the collection class is also called the container class.

    The collection class is not the same as the array. The elements of the array can be values ​​of basic types or objects; and the collection can only store objects.

    Java's collection class is mainly derived from two interfaces: Collection and Map.

    Divide all Java collections into three major categories: Set collection, List collection, and Map collection.

image

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113986392