Php has a detailed description of regular expressions _ PHP Tutorial

Source: Internet
Author: User
Php provides a detailed description of regular expressions. Php (as the mainstream development language) inherits the consistent tradition of * NIX and fully supports regular expression processing. Regular expressions provide an advanced, but not intuitive string matching and processing. php (as the mainstream development language) inherits the consistent tradition of * NIX and fully supports regular expressions. Regular expressions provide an advanced but not intuitive method for string matching and processing. Anyone who has used regular expressions in PERL knows that regular expressions are very powerful, but they are not so easy to learn.
For example:

^. + @. + \... + $

This effective but incomprehensible code is enough to make some programmers have a headache (I am) or let them give up using regular expressions. I believe that after reading this tutorial, you can understand the meaning of this code.

Basic mode matching

Everything starts from the most basic. Pattern is the most basic element of a regular expression. they are a set of characters that describe character strings. The mode can be very simple. it is composed of common strings and can be very complex. special characters are often used to indicate characters in a range, repeated occurrences, or context. For example:

^ Once

This mode contains a special character ^, indicating that this mode only matches strings starting with once. For example, this pattern matches the string "once upon a time" and does not match "There once was a man from NewYork. Like a ^ symbol, $ is used to match strings ending in a given pattern.

Bucket $

This mode matches "Who kept all of this cash in a bucket" and does not match "buckets. When both the character ^ and $ are used, it indicates exact match (the string is the same as the pattern ). For example:

^ Bucket $

Only matches the string "bucket ". If a mode does not include ^ and $, it matches any string containing this mode. Example: mode

Once

And string

There once was a man from NewYork
Who kept all of his cash in a bucket.

Is matched.

In this mode, letters (o-n-c-e) are literal characters, that is, they indicate the letter itself, and numbers are the same. Escape sequences are used for other slightly complex characters, such as punctuation marks and white characters (spaces and tabs. All escape sequences start with a backslash. The escape sequence of the tab is :. So if we want to check whether a string starts with a tab character, we can use this mode:

^

Similarly, a new line is used to indicate a carriage return. Other special symbols can be used in front with a backslash. for example, the backslash itself is represented by \, periods, periods, and so on.

Character cluster

In INTERNET programs, regular expressions are usually used to verify user input. After a user submits a FORM, it is not enough to determine whether the entered phone number, address, EMAIL address, and credit card number are valid.

Therefore, we need to use a more free way to describe the mode we want. it is a character cluster. To create a character cluster that represents all vowel characters, put all the vowel characters in a square bracket:

[AaEeIiOoUu]

This mode matches any vowel character, but can only represent one character. The font size can be used to indicate the range of a character, for example:

[A-z] // match all lowercase letters
[A-Z] // match all uppercase letters
[A-zA-Z] // match all letters
[0-9] // match all numbers
[0-9.-] // match all numbers, periods, and periods
[F] // match all white characters

Similarly, these are only one character, which is very important. If you want to match a string consisting of a lowercase letter and a digit, such as "z2", "t6", or "g7 ", if it is not "ab2", "r2d3", or "b52", use this mode:

Http://www.bkjia.com/PHPjc/508656.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508656.htmlTechArticlephp (as the mainstream development language) inherits * NIX's consistent tradition and fully supports regular expression processing. Regular expressions provide an advanced, but not intuitive string matching and processing...

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.