Ask the meanings of the suffixes of various regular expressions-php Tutorial

Source: Internet
Author: User
Tags email string
Ask the meanings of various regular expression suffixes about the meaning of the regular expression suffixes in php. now I know that the utable is not greedy, and I indicates that the case sensitivity is ignored, s allows & quot ;. & quot; match the line break. But I don't know what other suffixes mean, so I 'd like to give you some advice on this. ------ Solution ------------------ use Perl-compatible regular expressions in PHP to ask the meanings of the suffixes of various regular expressions
For the meaning of the regular expression suffix in php, I only know that the/u table is not greedy,/I indicates that the case is ignored,/s can make "." match the line break. But I don't know what other suffixes mean, so I 'd like to give you some advice on this.

------ Solution --------------------
Use regular expressions compatible with Perl in PHP
--------------------------------------------

Source: PHP power online
1 Preface
PHP is widely used in Web background CGI development. it usually produces some results after user data. However, if the user input data is incorrect, a problem may occur, for example, a person's birthday is "August February 30 "! How can we check whether the summer vacation is correct? We have added support for regular expressions in PHP, so that we can easily perform data matching.

2. what is a regular expression:
In short, regular expressions are a powerful tool for pattern matching and replacement. Trace regular expressions in almost all UNIX/LINUX-based software tools, such as Perl or PHP scripting languages. In addition, the script language of the JavaScript client also provides support for regular expressions. now, regular expressions have become a common concept and tool and are widely used by various technical personnel.
On a Linux website, there is a saying like this: "If you ask Linux fans what they like most, he may answer regular expressions. if you ask him what he is most afraid, in addition to tedious installation configurations, he will definitely say regular expressions. "
As mentioned above, regular expressions seem very complex and scary. most PHP beginners will skip this section and continue the following learning, however, regular expressions in PHP can use pattern matching to find matching strings, determine whether a string meets the conditions, or use a specified string to replace matching strings, it's a pity that you don't study ......


3. basic syntax of a regular expression:
A regular expression is divided into three parts: separator, expression, and modifier.
Separators can be any character except special characters (for example "/! ", Etc.), the commonly used separator is "/". The expression is composed of special characters (see the following for special characters) and non-special strings, such as "[a-z0-9 _-] + @ [a-z0-9 _-.] + "can match a simple email string. Modifier is used to enable or disable a function/mode. The following is an example of a complete regular expression:
/Hello. +? Hello/is
The above regular expression "/" is the separator, the expression is between two "/", and the string "is" after the second "/" is the modifier.
If the expression contains delimiters, you need to use the escape symbol "\", such as "/hello. +? \/Hello/is ". In addition to separators, escape characters can also be used to execute special characters. all special characters consisting of letters must be escaped by "\". for example, "\ d" indicates all numbers.


4 Special characters of the regular expression:
Special characters in regular expressions include metacharacters and positioning characters.
Metacharacter is a special character in a regular expression. it is used to describe how a primary character (the character before the metacharacter) appears in a matched object. Metacharacters are single characters, but different or identical metacharacters can be combined to form large metacharacters.
Metacharacters:
Braces: braces are used to precisely specify the number of occurrences of matching metacharacters, for example, "/pre {}/" indicates that the matched objects can be "pre", "pree", and "preeeee". In this way, one to five "e" appear after "pr ".. Or "/pre {, 5}/" indicates that pre appears between 0 and 5 times.
Plus sign: the "+" character is used to match the character before the metacharacters once or multiple times. For example, "/ac +/" indicates that the matched object can be "act", "account", and "acccc". one or more "c" objects appear after "". string. "+" Is equivalent to "{1 ,}".
Asterisk: "*" is used to match zero or multiple times before the metacharacters. For example, "/ac */" indicates that the matched object may be "app", "acp", and "accp". there may be zero or multiple "c" after "". "*" Is equivalent to "{0 ,}".
Question mark :"? "Character is used to match the character before the metacharacter zero or one time. For example, "/ac? /"Indicates that the matched object can be" a "," acp ", and" acwp ". In this way, zero or one" c "string appears after". "? "There is also a very important role in regular expressions, that is," greedy mode ".

There are two important special characters: "[]". They can match any character in "[]", for example, "/[az]/" can match a single character "a" or "z "; if you change the expression above to "/[a-z]/", you can match any single lowercase letter, such as "a" and "B.
If "^" is displayed in "[]", this expression does not match the characters in, for example, "/[^ a-z]/" does not match any lower-case letters! In addition, the regular expression provides the default values:
[: Alpha:]: match any letter
[: Alnum:]: match any letter or number
[: Digit:]: match any number
[: Space:]: matches space characters.
[: Upper:]: match any uppercase letter
[: Lower:]: match any lowercase letter
[: Punct:]: match any punctuation marks
[: Xdigit:]: match any hexadecimal number

In addition, the following special characters indicate the following meanings after the escape symbol:
S: matches a single space character.
S: used to match all characters except a single space character.
D: used to match numbers from 0 to 9, which is equivalent to "/[0-9]/".
W: used to match letters, numbers or underscores, equivalent to "/[a-zA-Z0-9 _]/".
W: used to match all characters that do not match w, equivalent to "/[^ a-zA-Z0-9 _]/".
D: used to match any non-decimal numeric characters.
.: Used to match all characters except line breaks. if the modifier "s" is modified, "." can represent any character.

The special characters above can be used to easily express some complicated pattern matching. For example, "/\ d0000/" can use the above regular expression to match an integer string of more than 100,001 and.

Positioning character:
Positioning characters are another very important character in regular expressions. They are mainly used to describe the position of characters in matching objects.
^: Indicates that the matching mode appears at the beginning of the matching object (different from that in)
$: Indicates that the matching mode appears at the end of the matching object.
Space: indicates that the matching mode appears at one of the two boundaries at the beginning and end.
"/^ He/": it can match strings starting with "he", such as hello and height;
"/He $/": Can Match strings ending with "he", that is, "she;

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.