What is the internal working mechanism of the regular expression engine
This article is about how the regular expression engine works internally. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Understand the need for a regular expression engine:
Knowing how the regular expression engine works helps you quickly understand why a regular expression doesn't work as you expect.
Types of regular expression engines:
There are two types of engines: text oriented (text-directed) engines and regular oriented (regex-directed) engines. Jeffrey Friedl calls them DFA and NFA engines. This article is about regular-oriented engines. This is because some very useful features, such as "lazy" quantifiers (lazy quantifiers) and backreferences (backreferences), can only be implemented in regular-oriented engines. So it's not surprising that this kind of engine is the current engine.
You can easily tell whether the engine you are using is text-oriented or regular-oriented. If backreferences or "lazy" quantifiers are implemented, you can be sure that the engine you are using is regular-oriented. You can do the following test: apply regular expressions to the string "regex not". If the result of the match is regex, the engine is regular-oriented. If the result is regex not, it is text-oriented. Because the regular-oriented engine is "urgent", it will be eager to show its work and report the matches it finds.
A small summary of the regular expression engine:
Regular-oriented engines always return the leftmost match, which is an important point to understand: even if it is possible to find a "better" match in the future, regular-oriented engines always return the leftmost match.
When applied to "He captured a catfish for his cat", the engine first compared to "H" and failed. So the engine failed to compare it with "e". Until the fourth character matches "c". Matches the fifth character. To the sixth character failed to match "p", also failed. The engine continues to re-check the match from the fifth character. Until the fifteenth character starts and matches the "cat" in "catfish", the regular expression engine eagerly returns * matches instead of looking for other better matches.
Thank you for reading! This is the end of this article on "what is the internal working mechanism of the regular expression engine". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!