1. Many people are confused about regular expressions .. Many symbols are combined. For example, the regular expression is the same as the regular expression, so let's look at what these symbols mean. Some symbols are not literally seen by everyone: for example, ","!", "?" ...... These symbols are called metacharacters. The meanings of these characters must be kept in mind: all the content in the red and bold Chinese quotation marks in this article is a regular expression, rather than general characters and symbols, we will explain "\" one by one: escape characters, which are still metacharacters in the character group. When checking a line of text, "^": delimiters: indicates the start of a line "$": Dollar sign: indicates the end character group of a line "[]": A pair of brackets, you can add any content, such as [hate], and the content in the brackets is or. The regular expression indicates that I want to match a character, which can be h, it Can Be a, t, or e. Remember: character groups only match one character. Even if your target string is hate, the result of a successful match is only the first letter h, not the entire word. What if I want to match the word hate? It is easy, the regular content is "hate", and the relationship between characters outside the character group is "and. Note: The metacharacters in the character group are not the same as those outside the character group. Even if the characters are the same, the meanings of the metacharacters may not be the same (*). First, we will learn the next content, next, let's explain the above sentence "[^]": It starts with "^" in brackets and is a negative form of character groups, for example, [^ hate: matching other characters except h, a, t, and e will still match only one character. I learned "^" to start a row, however, the starting position of the delimiters in [] indicates no. This also explains the content of (*). If the delimiters are in the brackets, what other positions do they represent? It indicates itself. At this time, it is no longer a metacharacter "-": minus sign. It can represent the range and range in the character group. For example, "[a-z]" indicates matching any lowercase letter directly from a to z. If it is "[-z]", "-" indicates only itself, it has no special meaning and does not have any special meaning outside the character group. Ps: other usage of "^", "$", and "-" will be described in the advanced basic article ".": point number. It is easy to write a character group that matches any character. ". "Is not a metacharacter ps in the character group:". "is not omnipotent, and some characters cannot be matched. Later, we will explain in detail and provide an alternative solution" | ": vertical line, indicating or, which is easy to remember. If you want to match hate or like, the regular expression is "hate | like". Note: If you want to match I hate you and I like you. The regular expression is "I (hate | like) you ". If it is written as "I hate | like you", the matching result is "I hate" or "like you". Here, parentheses are used to control the scope of the vertical bars. "() "There are a lot of functions, and I will talk about it later (for traditional NFA, the selection structure is in order, not matching first or ignoring first. For NFA, see getting started with regular expressions (java) -Matching Principle-1-engine classification and universality principle) "\ B": it serves as a word separator. If I want to match like, the regular expression is "like". That's right, but it will get some results that I don't want. For example, likely will also go to the result set. I don't want these words. Then, the regular expression "\ blike \ B" can only match the word like. Note: in java, the word separator is "\ B", and in some languages, the word separator is "\ <" and "\> ". What is the word boundary? In fact, regular expressions are not so intelligent. They do not recognize "Ilikeyou" as "I like you". They only find the start position and end position of numbers and letters. "\ w": word character. In java, it is equivalent to "[a-zA-Z0-9 _]". However, "\ B" IN java supports Unicode characters. Next let's take a look at the regular expression "Number"-quantifiers "?": 0 to 1 "*": 0 to any number of "+": indicates at least one "{min, max}": interval quantizer. "{2, 5}" indicates matching 2 to 5 times. "{2,}" indicates at least two times, and "{2}" indicates only two matches. "{, 2}", regular expression error, meaningless www.2cto.com For example: As mentioned in the previous reverse tutorial, if you want to match a positive integer, we need to clarify how to do it first: I don't want to match 0123. I just want to match a number like 123. 0 cannot start with it. The second requirement is that there are no other characters except numbers. We have learned character groups before, "[0-9]" can represent any number between 0 and 9. Then, based on the quantifiers above, we correct the value as "[0-9] + ", it indicates that there is at least one number, and there is no limit at most. However, the number 0123 meets the requirements. So let's modify it again. First, the first digit cannot be 0. Then, this digit can be written as "[1-9]", indicating that it matches any number ranging from 1 to 9. after that? It doesn't matter if there is any number next to it. Yes, that is, Multiple Digits. No, that is, one digit. Therefore, the regular expression is "[1-9] [0-9] *". Here is an example of a quantizer. For example, if I want to match a word, the regular expression can write "[a-zA-Z] + ". Each time RegexBuddy matches the color cross, it will be able to know what is matched every time. Let's look at the first result. there is a problem with "there: "\ w +" indicates at least one "\ w". Why is the result not "t", "h", "e", "r", or "e ", it is "there ". The quantifiers above, except for the specified number of matches such as "{times}", are greedy by default. That is to say, as many matches as possible. If there is a relatively inert match, how can we use it? The following example is modified: "\ w * e" indicates the word ending with e. Here, * is greedy. If I want to match the first e in the word, how can I modify it? Let's look at there. This time, "\ w +" only matches "th" and does not match "ther" to stop. The inert match is as few as possible. The method is to add "?" After the quantifiers. If there is no "?" And other quantifiers, It is the default greedy match. "?", "*", "+": Also called matching priority quantifiers "*?", "+ ?", "?": It is also called the ignore preference. In fact, there is another quantizer: "? + "," * + ", And" ++ ": take precedence. (There are few regular engines that support such quantifiers, which are supported by java.) This section does not discuss such quantifiers. The following chapter discusses (): using the content in parentheses as an independent unit, as a group. Reverse reference of "\" + numbers: many languages can remember the Matching content in "()", such as java. If I only want to use the content that matches "()", you can use reverse references. For example, if I want to find two identical words, I may encounter hate errors when I accidentally write them. How should I write regular expressions? "(Hate) \ 1" can be used to match a common word. We can write it as "([a-zA-Z] +) \ 1 ". In this way, errors in the form of hate can also be found. Note: Here ([a-zA-Z] +) represents at least one English letter, and \ 1 at the end does not represent at least one English letter, but is equal () the matching results are consistent! "([A-zA-Z] +) \ 1" is not "([a-zA-Z] +) ([a-zA-Z] +) for example, if the target string is likely like, "([a-zA-Z] +)" matches likely and a space, "\ 1" does not match "([a-zA-Z] +)". Because the result of the brackets is not like, this search fails and no result is returned. For example, "(\ d) \ d" matches the number 123. Then the result of "\ 1" is the first parentheses that are met, that is, the outermost, and the result of "\ 1" is 12. In the order of "\ 2", the result is 1, and the result of "\ 3" is 2, and then "()" is added. The reverse reference begins with another function of brackets, that is to save the captured content. This type of parentheses is called the capturing parentheses, which correspond to non-capturing parentheses "(? :) "" (? :) ": Non-capturing parentheses. "(?: Hate) "will match the hate string, but if you attempt to use reverse reference" (?: Hate) \ 1 ", then an error occurs. This operation is invalid. Note: Non-capturing parentheses do not affect the capture Count. Get "(? The content captured by "(\ d)" in "\ d) (\ d)" is \ 2, which is no longer a benefit of \ 3: 1. it is easy to understand to improve efficiency. If you do not remember the captured content, you will not occupy the memory. 2. "\ s", a common white space character for clear structure, indicates all white spaces. "\ T": tab. "\ N": line break. "\ R": Press enter. Some other commonly used thumbnails represent "\ S": any character except "\ s", "\ w": equivalent to [a-zA-Z0-9 _] "\ W ": any character except "\ w": equivalent to [0-9] "\ d": any character other than "D" is not supported by some tools, for example, searching in EditPlus v3.10 does not support \ d. Loop view (zero-width assertion) is divided into order and reverse order, positive and negative, a total of four combinations. Next let's take a look at what "(? (From left to right) to view the text. If the text can be matched, a successful match is returned. "(? <=) ": Positive backward view: (from right to left) view the text. If the text can be matched, a successful match is returned. "(?!)": Sequential negative view: (from left to right) view the text. If the text cannot be matched, a successful match is returned. "(? <!)": Negative backward view: (from right to left) view the text. If the text cannot be matched, a successful match is returned. Let's take a look at a few simple examples, and then it's easy to understand the above Martian language example: Here are two sentences: join in and you just want to find the book, don't want to find booksthere is a book on the desk. there are some books on the desk. the simplest method is book \ B, which is easy to understand. book is followed by the word separator. If book is followed by s, it must be considered a word, so it does not match books. How should I write a book (?! \ W) "book in the regular expression is very easy to understand, in order to match B, o, o, k, then, \ w in the above said equivalent to [a-zA-Z0-9]," (?! \ W) "means: I want to find such a position, which cannot be followed by \ w. In the first sentence, after matching the book, we found a space followed. Well, not the content in \ w. The match was successful. Note: loop view does not occupy characters! The Link View searches for locations between characters. Parentheses do not have the function of capturing characters, so reverse references cannot be used. The position of the triangle number is the location matched by the view. What if I want to match the book of books (? = S) "the above demonstrates Positive and Negative order. The following shows that the view does not occupy characters. It is easy to understand. This example has no practical significance. Some meaningful examples will be given after demonstration. regular Expression mode and matching mode. The content of this part is basically from "proficient in regular expressions" v31. case-insensitive mode: (Pattern is used in java. CASE_INSENSITIVE) 2. loose arrangement and comment mode: This mode ignores all blank characters outside the character group, but in java. util. in regex, all blank characters outside the character group are not ignored, but are considered as "do-nothing metacharacter )". Regular Expression "\ 12 3" table "3" after "\ 12", rather than "\ 123" (Pattern is used in java. COMMENTS) ps: the definition of "white space" depends on the definition of the character encoding used, and the degree to which this encoding supports white space characters. Most programs can only recognize ASCII spaces. 3. Point/number wildcard mode (single line mode): (dot-match-all match mode) generally, the point/number cannot match the line break. However, in java, It is special: in sun's regular expression package, the point number can match all single-character Unicode line Terminators that do not use this mode. (Pattern is used in java. DOTALL) character: unicode line terminator character description lf u + 000AVT U + 000BFF U + 000cr U + 000DCR/lf u + 000D U + 000 anel u + 0085LS U + 2028 ps u + 2029 ASCII line break ASCII vertical tab ASCII paper character ASCII carriage return/line feed Unicode line separator Unicode segment separator 4. enhanced anchor match mode: (Enhanced line-anchor match mode) This mode affects matching between "^" and "$. Generally, "^" cannot match the linefeed inside the string, but can only match the starting position of the target string. In this enhancement mode, it can match the starting position of the text line embedded in the string. The same is true for "$". In this mode, the line break inside the string can be matched. It may not be easy to understand here. The subsequent examples will introduce the meanings of "\ A", "\ Z", and "\ z" (Pattern is used in java. CASE_INSENSITIVE) 5. text mode: This mode can hardly recognize any metacharacters. In this mode, the regular expression "[a-z] +" matches the string "[a-z] +" instead of consecutive lowercase English letters (Pattern is used in java. CASE_INSENSITIVE) If you do not understand the above several modes, you can refer to the subsequent tutorials. It will be discussed in basic instances and advanced basics.