This article mainly introduces the definition and usage of backtracing in regular expressions, analyzes the concept and functions of backtracing in combination with examples, and provides JS and java implementation methods, you can refer to the examples in this article to analyze the definition and usage of regular expressions. We will share this with you for your reference. The details are as follows:
I am also the first contact with "backtracking", and I am not very familiar with it. The following describes what I know as a mental log for future reference.
The basis for matching the regular expression we use is roughly divided into: the matching result with the leftmost end (starting with the most) and the standard matching quantifiers (*, + ,? And {m, n}) are matched first.
As the name suggests, "first-left matching is to start matching from the starting position of the string until the end of the matching." standard matching quantifiers "are also divided into" uncertain type finite automaton (NFA) "It can also be called" expression-dominated ". The other is" deterministic finite automaton (DFA) "or" text-dominated ". The regular expression we currently use in JavaScript is "expression-dominated ". Expression-dominated and text-dominated are difficult to explain. First, an example may be clearer.
// Use the regular expression to match the text var reg =/to (nite | knight | night)/; var str = 'doing tonight '; reg. test (str );
In the above example, the first element [t] will repeat until 't' is found in the target string. Then, check whether the followed character can be matched by [o]. If yes, check the following elements (nite | knight | night ). Its true meaning is "nite", "knight", or "night ". The engine tries these three possibilities in sequence. The process of trying [nite] is to first try [n], then [I], then [t], and finally [e]. If this attempt fails, the engine tries another possibility, and continues until the matching is successful or the report fails. The control in the expression is converted between different elements, so it is called "expression-dominated ".
Similarly, in the preceding example, "text-dominated", all valid matches are recorded during string scanning. When the engine moves to t, it will add a potential possibility to the matching possibilities currently processed:
Example 1: extract the string da12bka3434bdca4343bdca234bm to extract numbers between character a and character B, but the character before a cannot be c, and the character after B must be d.
For example, only the number 3434 meets the requirements. So how can we extract it?
First, we write the expression for extracting this string :(?
The Java code snippets are as follows:
Pattern p = Pattern. compile ("(?
Example 2: truncates multiple decimal places to three decimal places: \ d + \. \ d [1-9]? \ D +
In this condition, 6.625 can be matched. This is unnecessary because it is a three-digit decimal. The last "5" was originally matched for [1-9], but there is another \ d +. Therefore, [1-9] Is "?". You can only discard the current match because it does not match. Send "5" to \ d + for matching. If it is changed:
\d+\.\d\d[1-9]?+\d+
When "5" matches [1-9], because it is intrusive, it will not be traced back, And the \ d + will not match anything, therefore, the 6.625 match fails.
In this case, the replacement takes effect. For example, if the number is truncated to three digits after the decimal point, if the number is exactly three decimal places, the replacement is unnecessary, which improves the efficiency, encroaching quantifiers are basically used to improve matching efficiency.
Set \ d + \. \ d [1-9]? + \ D + to \ d + \. \ d (?> [1-9]?) \ D + is the same.
I hope this article will help you design JavaScript programs.
For more information about the definition and usage of backtracking in regular expressions, see [JS and java implementations!