A solution to the meaning of various regular expression suffixes

Source: Internet
Author: User
Tags uppercase letter
Ask for the meaning of various regular expression suffixes
About the meaning of the expression suffix in PHP, now I am smattering, just know/u table non-greedy match,/I means ignoring case,/s can let "." Match to line break. But do not know what the other suffixes have meaning, so want to ask the system is also looking at this aspect of the predecessors advice.

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

Source: PHP Power Online
1 Preface
PHP is widely used in the background CGI development of the web, usually after the user data data to obtain a certain result, but if the user input data is incorrect, there will be problems, such as someone's birthday is "February 30"! What should I do to check whether the summer vacation is correct? With the support of regular expressions in PHP, we can make data matching very convenient.

2 What is a regular expression:
In short, regular expressions are a powerful tool that can be used for pattern matching and substitution. Find traces of regular expressions in almost all software tools based on the Unix/linux system, such as Perl or PHP scripting languages. In addition, JavaScript, a client-side scripting language, also provides support for regular expressions, and now regular expressions have become a common concept and tool that is widely used by various technical staff.
On one of the Linux sites, "If you ask what Linux enthusiasts like best, he may answer regular expressions, and if you ask him what he fears most, he will say regular expressions in addition to the cumbersome installation configuration." "
As said above, the regular expression looks very complex, scary, most of the PHP beginners will skip here, continue the following learning, but the regular expression in PHP has the ability to use pattern matching to find a qualifying string, It is a pity to judge whether a string is eligible or to replace a string with a specified string, such as a strong function.


3 basic syntax for regular expressions:
A regular expression, divided into three parts: delimiters, expressions, and modifiers.
The delimiter can be any character other than a special character (such as "/!"). And so on), the commonly used delimiter is "/". Expressions are made up of special characters (see below for special characters) and non-special strings, such as "[a-z0-9_-]+@[a-z0-9_-.] + "can match a simple e-mail string. Modifier is used to turn on or off a function/mode. Here is an example of a complete regular expression:
/hello.+?hello/is
The above regular expression "/" is the delimiter, between two "/" is an expression, the second "/" after the string "is" is the modifier.
If you have delimiters in an expression, you need to use the escape symbol "\", such as "/hello.+?\/hello/is". Escape symbols can execute special characters in addition to delimiters, and all the special characters made of letters need to be "\" to escape, for example "\d" represents all numbers.


4 special characters for regular expressions:
Special characters in regular expressions are divided into metacharacters, positional characters, and so on.
Metacharacters is a special kind of character in a regular expression that describes how the leading character (the character preceding the meta character) appears in the matched object. The meta-character itself is a single character, but different or identical meta-characters can be combined to form large meta-characters.
Metacharacters
Curly braces: Curly braces are used to precisely specify the number of occurrences of a match metacharacters, such as "/pre{1,5}/" to indicate that a matching object can be a "pre", "Pree", "preeeee" so that a string of 1 to 5 "E" appears after PR. or "/pre{,5}/" on behalf of the pre appears 0 this to 5 times between.
Plus: the "+" character appears one or more times before a character is used to match a meta character. For example, "/ac+/" means that the matched object can be "act", "account", "ACCCC", and so on, after "a", one or more "C" strings. "+" equals "{1,}".
Asterisk: "*" character is used to match metacharacters before characters appear 0 or more times. For example, "/ac*/" means that the matched object can be "app", "ACP", "ACCP" and so on "a" after "a" appears 0 or more "C" string. "*" corresponds to "{0,}".
Question mark: "?" Characters appear 0 or 1 times before the character is used to match a meta character. For example, "/ac?/" means that the matching object can be a "a", "ACP", "ACWP" so that a 0 or 1 "C" string appears after "a". "? " There is also a very important role in regular expressions, namely "greedy mode".

There are also two very important special characters that are "[]". They can match the characters that appear in "[]", such as "/[az]/" can match a single character "a" or "Z", if the above expression is changed to such "/[a-z]/", you can match any single lowercase letter, such as "A", "B" and so on.
If "^" appears in "[]", it means that this expression does not match the characters appearing in "[]", such as "/[^a-z]/" does not match any lowercase letters! And the regular expression gives the default values of several "[]":
[: Alpha:]: Matches any letter
[: Alnum:]: Matches any letter and number
[:d Igit:]: Matches any number
[: Space:]: Match whitespace
[: Upper:]: matches any uppercase letter
[: Lower:]: matches any lowercase letter
[:p UNCT:]: matches any punctuation
[: Xdigit:]: Matches any 16 binary digits

In addition, the following special characters are escaped after escaping the symbol "\" to represent the following meanings:
S: matches individual whitespace
S: Used to match all characters except a single space character.
D: Used to match numbers from 0 to 9, equivalent to "/[0-9]/".
W: Used to match letters, numbers, or underscore characters, 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 numeric characters that are not 10 binary.
.: Used to match all characters except the newline character, if the modifier "s" is decorated, "." can represent any character.

The use of the above special characters can be very convenient to express some of the more cumbersome pattern matching. For example, "/\d0000/" uses the above regular expression to match the integer string above, 100,001.

Positioning characters:
A positional character is another very important character in a regular expression, and its main purpose is to describe the position of the character in the matching object.
^: Indicates that the matching pattern appears at the beginning of the matching object (and differs in "[]")
$: Indicates that the matching pattern appears at the end of the matching object
Spaces: Indicates that the matching pattern is one of the two boundaries at the beginning and end
"/^he/": You can match a string that begins with the "he" character, such as Hello, height, and so on;
"/he$/": You can match a string that ends with the "he" character, she, and so on;
  • 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.