Regular expressions-1. Understanding of the principle

Source: Internet
Author: User
Tags posix regular expression expression engine egrep

A regular expression (regular expression called regex) is a tool that is invented to solve a particular type of problem and is a string used to match and manipulate text.

the core of a regular expression is the way the regular expression engine is processed.

The general regular expression engine is divided into two types: NFA and DFA.

But the NFA has two more branches: traditional NFA and POSIX NFA the following are the supported engines in various languages:


traditional NFA: GNU Emacs, Java, grep, less, more,. NET, Perl, PHP, Python, Ruby, sed, etc.
DFA: awk, Egrep, Flex, MySQL, Procmail
POSIX NFA: Mawk, Mortice Kern, and more.
DFA/NFA hybrid engine : GNU awk, GNU Grep/egrep, TCL

The general DFA is faster, and the NFA has strong control capability.



The DFA engine does not require backtracking, so the match is fast, but the capturing group is not supported, so it does not support reverse referencing and the use of this method of referencing.


Most languages and tools use the traditional NFA engine, which has some features that are not supported by DFA:

Capturing groups, reverse references, and the method of referencing in the same way;

Surround (Lookaround, <= ...), (?<!...), (? = ...), (?! ...), or some an article called pre-search;

Ignore optimization quantifiers (??、 *, +, {m,n}, {m,}?), or some articles called non-greedy patterns;

Occupy the priority quantifiers (? +, *+, + +, {m,n}+, {m,}+, currently only Java and pcre support), curing group (...).


two. Regular processing has roughly four steps


First: Compile, when you create a regular Object Browser will convert it into a native code to perform matching work
Second: Set the starting position. When the regular class enters the practical state, first determine the starting search location of the target string
Third: match each regular expression character. Once the regular expression knows where to start, he checks the text and the regular expression pattern one at a- When a particular character match fails, the regular tries to go back to where it was before trying to match, and then try the other possible paths
IV: Announce a match success or failure


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.