Regular Expressions is not a Java patent. Many languages such as Perl, Python, PHP, and Ruby support Regular Expressions. Regular Expressions are a powerful tool for string processing, it is a sub-expression describing the string mode. The core value of a regular expression is to match a string. The regular expression engines implemented in different languages are not exactly the same. "proficient in regular expressions" published by Oreilly is a classic tutorial on regular expressions. Here we only summarize the knowledge about regular expressions in Java. The Regular Expression Function in Java is implemented through java. util. two classes in the regex package are implemented: The Pattern class, which defines objects that encapsulate regular expressions; The Matcher class, which defines objects that encapsulate a state machine, this state machine can use a given Pattern object to search for a specific string. This package also defines the PatternSyntaxException class. If a syntax error is found when a regular expression is compiled to create a Pattern object, an exception is thrown. A regular expression is also a String. It is generally encapsulated into a Pattern object. In some simple cases, you can use only the matches () of the String class instead of the regex package () you can determine whether the string matches the regular expression. For example:
"One piece". matches ("one. *"); // true
Using the Regular Expression in java is basically simple:
(1) pass a string containing a regular expression to the static method compile () of the Pattern class to create a Pattern object.
(2) Call the matcher () method of the Pattern object and use the string to be searched as the real parameter to obtain a Matcher object.
(3) Call the find () method of the Matcher object to search for strings.
(4) If this mode string is found, you can query the Matcher object to find the position of the mode string and other information related to its matching.
The above four steps are described in the book "getting started with Java2" --- Ivor Horton. These steps guide us to learn Regular Expressions: 1. the core thing is writing regular expressions. That is to say, you must master the regular expression syntax to write the pattern of the string to be matched. For example, you must match an email address and how to write the regular expression. 2. Understand the functions of matcher (), find (), compile () and other methods. In fact, it is to check the API and see what these method parameters are, what the return value is, and what the role is. 3. Regular Expression Programming is based on certain steps. Now the learning routine is quite clear. Simply put, you can grasp the syntax-> understand the method-> follow the steps.
The first step is the syntax of Regular Expressions in Java. Here we clarify the context of the syntax. The specific syntax is just as complete as baidu, wikipedia and other search engines can directly find detailed explanations about regular syntax. Regular expressions have the following syntax elements: regular characters, character classes (Character Set combination), wildcards, quantifiers, boundary match characters, operators, groups, and flag sequences. Use examples to help clarify the context of the regular expression syntax.
For example, if there is a string "0310 handan", we need to write a regular expression to describe this string. What is the simplest way? Write 0310 handan (check the API and you will see that the compile parameter of the method is String regex. The regular expression is given in the form of a String, but it is not a String in itself, to highlight the syntax features of regular expressions, regular characters are not enclosed in quotation marks. To do this, the regular expression is mapped one by one. A string corresponds to a regular expression. We say that the regular expression is a powerful tool to describe the string, which means two points: 1. A regular expression can describe multiple or even infinite strings, which reflects its powerful description. 2. A string can be expressed as multiple regular expressions, which reflects its flexibility. It is a powerful and flexible tool ". The following syntax features are used to achieve these two points. The matching process is to match characters one by one. For example, "0310 handan" is written as a regular expression, which is 10 unit items, which correspond to 0, 3, h, a, n, d, what syntax features can be added to a, n to allow a unit item in the regular expression to match many characters, the idea is to use a unit item to represent a set of many common characters. This unit item is called a character class. For example, [xyz] can match x, y, or z, [^ xyz] matches any character other than x, y, and z, [a-z] matches all lower-case letters, and \ d matches numbers 0-9. The Java Regular Expression API provides a wide range of predefined character classes to express this idea. The following are commonly used: \ d, \ D, \ w, \ W, \ s, \ S. These are quite memorable, because they are too often used, so the logging method is simple. Java also provides a large number of character classes with the following general forms, \ p {name }, name specifies the name of the class. The following are examples:
\ P {Lower} contains lowercase letters
\ P {Upper} contains uppercase letters
\ P {Punct} contains all punctuation marks
Regardless of the log method, all of these indicate a unit item, used to match a character, and missing a "Big Man", which is a wildcard character. It is written as a dot ". "Why is it a big man? It can match all characters.
Although I have been familiar with it, I have some doubts. I have a strong expression ability, but it is still very troublesome to write it. Take "0310 handan" as an example, we still need to write 10 unit items, but each unit item has a variety of optional writing methods, such as writing... \ d ..... \ p {Lower}, but the write is not longer. I can't help but sigh that this regular expression has been learned in vain, Which is troublesome. This article introduces a traditional syntax feature in regular expressions, that is, quantifiers, which are divided into greedy quantifiers, stress quantifiers, and occupy quantifiers. Quantifiers are very useful and easy to use. It determines how many times a unit item will match. Simply put, multiple identical unit items can be merged, therefore, the above statement is syntactically equivalent to "0310 handan". In practice, it is impossible to do this, because every character should be represented in a uniform form as much as possible, in this way, you can use a quantizer to write more concisely. For example, you can use this formula to match the string: \ d \ w. After applying the quantifiers, you can write the following quantifiers: \ d + \ w {6:
+ Match once or multiple times
* Match 0 times or multiple times
? Match 0 times or 1 time
These three are very common, but will they be enough if they are enough? Obviously, this does not specify the specific quantity. {num} can be used to specify the number of times. For example, x {2} matches "xx ". Is it enough now? The fixed number of times is not flexible enough. You need to specify the minimum number and the maximum number of {min, max}. You can also choose not to specify max here, for example, x {2 ,} can match "xx", "xxx", "xxxx", and so on. The types of quantifiers seem to be enough now. In fact, we haven't introduced it yet. We have been talking about greedy quantifiers so far. What is the problem of coercion quantifiers and possession quantifiers? I will explain it later.