Introduction to Regular Expressions

Source: Internet
Author: User

Readers who are new to regular expressions will not only feel complicated, but also have an unmeasurable feeling. In fact, regular expressions are a custom syntax rule that describes the character arrangement mode. In the system functions provided by PHP, this mode is used to match, search, replace, and split strings. It is widely used. For example, a regular expression is usually used to verify whether the user's username, password, e-mail address, ID card number, and phone number submitted in the form are legal. When a user publishes an article, add the corresponding connections to all the URLs entered, calculate the total number of sentences in the article based on all punctuation marks, and capture data in a certain format on the webpage. Regular expressions are not a product of PHP. They are used in many fields. Regular Expressions are not a product of PHP. They are used in many fields, in addition to Perl, C #, and Java, in our B/S architecture software development, regular Expressions can be used in Linux, foreground JavaScript scripts, background scripts PHP, and MySQL databases.

A regular expression is also called a pattern expression. It has a complete syntax system that can write patterns and provides a flexible and intuitive string processing method. Regular Expressions match, search, replace, and split strings by constructing a pattern with specific rules and comparing it with input string information in specific functions. The following example provides three modes, which are constructed according to the regular expression syntax rules. As follows:

123 "/[a-zA-z]+://[^\s]*/" // Match the regular expression of the URL"/<(\S*?)[^>]*>/*?</\1>|<.*?/>/i" // Match the regular expression of the HTML Tag"/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w)*/" // Match the regular expression of the email address

Do not be intimidated by seemingly garbled strings in the above example. They are constructed according to the regular expression syntax rules. They are a string consisting of common characters and special characters. In addition, these pattern strings must be placed in a specific regular expression function for the effect.

In PHP, two sets of Regular Expression processing function libraries are supported. A set of Regular Expression functions provided by the PCRE Library that are compatible with the Perl language. Use a function named with the prefix "prefix", and the expressions should be included in the delimiters, such as diagonal lines (/). The other is a regular expression function with POSIX extension syntax, which is named with the prefix "ereg. The functions of the two function libraries are similar, and the execution efficiency is slightly different. Generally, to implement the same function, the efficiency of using the regular expression provided by the first PCRE Library is slightly higher. Therefore, this article mainly introduces the regular expression functions with the prefix "prefix.

 

> Fixed link: http://php.ncong.com/php_course/php_zend/chushizhengze.html

> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, July 28, 2014.

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.