Getting started with regular expressions (java)-matching principle-2-backtracking

Source: Internet
Author: User

The most important feature of the backtracking NFA engine is that it processes Each subexpression or composition element at a time and needs to select between two possible successes, it selects one of them and remembers other results for future selection, including quantifiers (deciding whether to try another match) and multiple-choice structures (deciding which multiple-choice branch to choose) two key points: 1. if you need to select between "try" and "Skip attempt", the engine will give priority to "try" for matching priority quantifiers. for ignoring priority quantifiers, will select "skip the attempt" www.2cto. com2. the option closest to the current storage is to forcibly return the result when a local failure occurs. The principle is LIFO (last in first out, followed by first in first out ). In fact, the NFA search process algorithm is depth-first (for details about depth-first, see the article at the end of the article. The content is from the Chinese machine encyclopedia), but it is not necessarily completely traversed, after the matching is completed, the search is stopped. Here are some simple examples to illustrate. For example, if we want to match the last two digits in a string, the target string "3456", regular "\ d + (\ d)", the following is a process: the matching process is relatively simple. First, \ d + matches 3, 4, 5, and 6. The green circle is the standby position of \ d +. \ D + continues to try to match and finds that there are no characters. Therefore, the match ends and the control is handed over to \ d. However, \ d cannot match, so Backtracking is required. The regular expression returns to the Second Green Circle, and the control is handed over to \ d. Now \ d can be matched to number 6. The match ends and the control is handed over to \ d. If no character is left to it, backtracking is required. The regular expression returns to the first green circle, and the control is handed over to \ d. Now \ d can be matched to number 5. The match ends. The control is handed over to \ d, the number 6 is matched, and the match ends. Now the entire expression is matched. (Here, the red circle indicates switching control, which is easy to understand. A new branch can be generated only in the Green Circle. In other places, if the match fails, you only need to return the original path to the green circle, and then try the quantifiers and the STANDBY state of the multiple choice structure) in the loop view, if the matching attempt of the loop view structure ends, it will not leave any STANDBY state. If the matching succeeds, it will discard the remaining STANDBY state. If the matching fails, it will continue to try the matching until all the standby states are used up, so no STANDBY state will be left. In the loop view, it is possible to give up the STANDBY state. The curing group and occupying priority measure described below will also be of this nature. The following is an obvious fact that is easy to ignore. Whether matching is preferred or ignoring, the engine will attempt all possibilities if the matching fails. Therefore, if there are too many backtracing possibilities, your program may be blocked and ANR will be generated in android. An example of blocking the program will be provided later. (For traditional NFA, the selection structure is sequential, not matching first or ignoring first) fixed grouping and take precedence quantifiers (?> ...): Curing group "? + "," * + "," ++ ", And" {m, n} + ": takes precedence quantifiers to solidify the group for" (?> ...)" In the content section (the Part omitted by the ellipsis), there is no difference with the previously matched rules, but when this part of expression matches, when starting to match the parts outside the brackets, all the standby states in the brackets will be abandoned. That is to say, if the subsequent matching fails, it will not roll back the status recorded before the curing group (because after the curing group is established, it will forget the previous status, and this buddy's memory is not very good ). Both the curing group and the loop view give up the backup status. We can consider using the affirmative loop view to simulate the curing group. For "(?> Regex) ", we want to discard the STANDBY state after matching the regex, we know" (? = Regex) "if an alternative state is abandoned after the matching is completed, you can use" (? = (?: Regex) \ 1 ", which is slower than the real curing group, because it needs to re-match" \ 1 ". The following is a simple example: the target string "abc", regular "(? = \ W +) \ 1 "first, \ w + will match abc. After the matching is complete, discard all of its alternative states and hand over the control to" \ 1 ". "\ 1" re-matches abc. If the regular expression is changed to "(? = \ W +) \ 1c "I want \ w + to match" AB ", so" \ 1 "matches" AB "and" c "corresponds to" c ", matched successfully. However, this is not the case! Like the above matching process: \ w + will match abc first. After the matching is complete, it will discard all its alternative states and give control to "\ 1 ". "\ 1" re-matches abc. Then, the control is handed over to "c". If the matching fails and there is no standby status, the overall matching fails. Some may think, what if I let the regular expression Go Back To The loop view? In fact, the same is true. when the control is handed over to the Central View, "\ w +" still directly matches "abc", and we will all know it later, and then trace back ...... Therefore, when "c" cannot match characters, there is no need to trace back and a matching failure can be directly declared. Let's take a look at this regular expression: "(?>. *?)" If the above content is understood, this regular expression is not difficult, and it will never match any character. A preference is similar to a match preference (Greedy match). The difference is that a preference does not return a character, but a match preference returns a character if needed. The following is an example: String: aaaaa regular 1: "\ w + a" regular 2: "\ w + + a" regular 1: first, the \ w + section of "\ w + a" will match all characters, and it will occupy 5 a characters, and then "\ w + a" will match a of them, if no character is left for it, \ w returns the previous character and one character each time. After one is returned, \ w has "aaaa". At this time, a of "\ w + a" finds that it can match the characters returned by \ w, so the match is successful and the match ends. Regular 2: Similarly, the \ w ++ section of "\ w ++ a" will match all characters, and then find that part a of "\ w ++ a" cannot match, however, \ w ++ does not return the matched characters. Therefore, the match fails! The authors tell us that the following two expressions must be distinguished: "(?> M) + "expression 2:" (?> M +) "expression 1 abandons the STANDBY state of M, but M does not create a state, so it makes no sense to do so. Expression 2 abandons the STANDBY state of M +, which obviously makes sense. Expression 3: "M ++" is the same as expression 2, which can be achieved by fixed grouping. The following is a slightly complex expression of possession priority. How can we convert it into a solidified group? (\ "| [^"]) * + In fact, if I understand the text above, the conversion is quite simple. The result is as follows (?> (\ "| [^"]) *) The author may think that many people may write the following incorrect format (?> \ "| [^"]) * Therefore, the author specifically summarized the following: remove the plus sign indicating priority, and use the curing group to include the remaining parts. Depth-First-Search category: Search Algorithm Data Structure: Image time complexity: space complexity: Optimal Solution: No Completeness: Yes others: A maximum depth search algorithm for B-branch coefficients m-graphs. It is the node that traverses the tree along the tree in depth, and the branch of the search tree is as deep as possible. When all the edges of node v have been explored, the search will trace back to the Start Node of the edge of node v. This process continues until all nodes that can be reached from the source node are found. If no node is found, select one of the nodes as the source node and repeat the above process until all nodes are accessed. It is a blind search.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.