Learn more about the regular expressions used to create PHP skills for dynamic web pages

Source: Internet
Author: User
Tags expression engine
Regular expressions are hard to write, difficult to browse, difficult to protect, often incorrectly match unexpected texts or Miss valid texts. These headers are caused by the expression and talent of regular expressions. Metacharacter

Regular expressions are hard to write, difficult to browse, difficult to protect, often incorrectly match unexpected texts or Miss valid texts. These headers are caused by the expression and talent of regular expressions. The talents of each metacharacter can be combined with nuances, which makes the code unable to be explained without the help of intellectual skills.

Many tools that contain certain features make it easy to browse and write regular expressions, but they are not suitable for habits. For many programmers, writing regular expressions is a magic art. They maintain what they know and are optimistic about it. If you are willing to use the five habits discussed in this article, you will be able to let the regular expressions you have designed survive repeated experiments.

This article uses Perl, PHP, and Python as code examples. However, the suggestions in this article are applicable to the performance of any interchange expression (regex.

I. apply spaces and comments

For most programmers, applying spaces and indentation in a regular expression environment is not a title. if they do not do this, they will be jokes by their peers or even layers. Almost everyone knows that it is difficult to browse, write, and protect code in a single line. What are the differences between regular expressions?

Most interchange expression tools have the extended space feature, which allows programmers to expand their regular expressions into multiple rows and add comments at the end of each line. Why does a small number of programmers use this feature? By default, regular expressions in Perl 6 expand spaces. Do not extend the space for your language by default. take the initiative to use it.

One of the tricks to remember to expand spaces is to make the regular expression engine neglect to expand spaces. In this way, if you need to match spaces, you have to clarify clearly.

In Perl, add x to the end of the regular expression, so that "m/foo | bar/" becomes the following situation:

M/
Foo
|
Bar
/X

In the PHP language, add x at the end of the regular expression, so that "'/foo | bar/'" becomes the following situation:

'/
Foo
|
Bar
/X'

In the Python language, the passing mode polishing parameter "re. VERBOSE" gets the compilation function as follows:

Pattern = r '''
Foo
|
Bar
'''
Regex = re. compile (pattern, re. VERBOSE)

When processing more complex regular expressions, spaces and comments are more important. Assume that the following regular expression is used to match the telephone number in the United States:

\(? \ D {3 }\)? ? \ D {3} [-.] \ d {4}

This regular expression matches the phone number such as "(314) 555-4000". do you think this regular expression matches "314-555-4000" or "555-4000? The answer is that the two do not match. Writing such a line of code hides faults and design results. The telephone area code is required, but the regular expression lacks clarification of a separator between the area code and the prefix.

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.