How regular expressions work _ PHP Tutorial

Source: Internet
Author: User
The working principle of parsing regular expressions. A regular expression is a single string used to describe or match a series of strings that conform to a certain syntax rule. In many text editors or other tools, regular expressions are used Regular expressionIs a single string used to describe or match a series of strings that conform to a certain syntax rule. In many text editors or other tools, regular expressions are usually used to retrieve and/or replace text content that meets a certain pattern.

Writing regular expressions is the main cause of performance bottleneck, but there are still many ways to improve the efficiency of regular expressions. Matching the same text with two regular expressions does not mean they have the same speed.

Many factors affect the efficiency of regular expressions. First, the text adapted to regular expressions varies widely, and the time required for partial matching is longer than that used for full non-matching. The regular expression engine of each browser also has different internal optimizations.

To effectively use regular expressions, it is important to understand their working principles. The following are the basic steps for processing a regular expression:

Step 1: Compile

After you create a regular expression object (using a regular expression or RegExp constructor), the browser checks whether your template is correct and converts it into a local code routine, used for matching. If you assign a regular expression to a variable, you can avoid repeating this step.

Step 2: Set the start position

When a regular expression is put into use, you must first determine the position where the search starts in the target string. It is the starting position of the string, or specified by the lastIndex attribute of the regular expression, but when it is returned from Step 4 (because the attempt to match fails ), this position is located at the start of the last attempt to push the next character.

The browser optimizes the regular expression engine by skipping unnecessary work through early prediction at this stage. For example, if a regular expression starts with "^", IE and Chrome usually determine whether a match can be made at the starting position of the string, and then avoid stupid searches for subsequent positions. Another example is to match the character string with the third letter "x". a clever way is to first find x and then backtrack the start position to two characters.

Step 3: match the characters of each regular expression

Once a regular expression finds its starting position, it will scan the target text and regular expression templates one by one. When a specific character fails to match, the regular expression will attempt to go back to the position before scanning, and then enter other possible paths of the regular expression.

Step 4: match successful or failed

If a full match is found at the current position of the string, the regular expression is declared successful. If all possible paths of the regular expression have been tried, but the match fails, the regular expression engine returns to step 2 and retries from the next character of the string. Only when each character in the string (and the position after the last character) has gone through this process and the matching has not been successful, then the regular expression will be declared as a complete failure.
Keeping this process in mind will help you identify the types that affect the performance of regular expressions wisely.

Original article address:Http://www.yiiyaa.net/1231


A string is a single string used to describe or match a series of strings that conform to a certain syntax rule. In many text editors or other tools, regular expressions are passed...

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.