PHP Regular Expression Learning (Appendix Video tutorial) _php Tutorial

Source: Internet
Author: User
Tags php regular expression posix vars
PHP regular expressions are primarily used for pattern segmentation, matching, finding, and substitution of strings. Using regular expressions can be inefficient in some simple environments, so how to better use PHP Regular expressions requires comprehensive consideration

My PHP is getting started, is the origin of an article on the Internet, this article is based on the use of PHP regular expression method, I think is a very good entry material, but the study still depends on the individual, in the process of use, or will continue to forget, so repeatedly read this article has four or five times, For some of the more difficult points of knowledge, and even a long time to digest, but as long as you can see the adherence to the reading, you will find that their ability to use the regular will be significantly improved.

Bkjia Video tutorial 36:php expression learning and application (I.)
Bkjia Video tutorial 37:php expression Learning and its application in Taiwan (II.)
Bkjia Video tutorial 38:php expression Learning and application in Zheng-zhong (iii.)
Bkjia Video tutorial 39:php expression Learning and application (IV.)

The definition of the PHP regular expression:

A syntax rule that describes the pattern of character arrangement and matching. It is used primarily for pattern segmentation, matching, finding, and substitution of strings.

Regular functions in PHP:

There are two regular functions in PHP, each of which has the same function:

The set is provided by the Pcre (Perl Compatible Regular Expression) library. A function named with the prefix "Preg_";

A set of extensions provided by the POSIX (portable Operating System Interface of Unix) extension. Use a function named "Ereg_" as a prefix (POSIX regular library, since PHP 5.3, is not recommended, after PHP6, will be removed)

Since POSIX is about to roll out the historical stage, and Pcre and Perl are similar in form, it's better for us to switch between Perl and PHP, so here's a focus on the use of pcre.

Pcre Regular Expressions

Pcre is all called Perl Compatible Regular expression, which means Perl-compatible regular expressions.

In Pcre, a pattern expression (that is, a regular expression) is typically included between two backslashes "/", such as "/apple/".

Some of the important concepts in the regular are: metacharacters, escapes, pattern units (repetitions), antisense, references, and assertions, all of which can be easily understood and mastered in article [1].

Common metacharacters (Meta-character):

Metacharacters description

A matches the atom of the string header

Matches an atom at the end of a string

Matches the boundary of a word/is/a string that matches the header of IS,/is/matches the string/is/bound for IS

B matches any character except the word boundary/bis/matches the "is" in the word "this"

D matches a number; equivalent to [0-9]

D matches any character except a number; equivalent to [^0-9]

W matches an English letter, number, or underscore; equivalent to [0-9a-za-z_]

W matches any character except English letters, numbers, and underscores; equivalent to [^0-9a-za-z_]

s matches a white space character, equivalent to [FV]

S matches any character except white space characters; equivalent to [^FV]

F matches a page break equivalent to x0c or CL

Match a line break; equivalent to x0a or CJ

Match a return character equivalent to x0d or CM

Matches a tab character, equivalent to x09 or \CL

V matches a vertical tab; equivalent to x0b or CK

Onn matches an octal number

XNN matches a hexadecimal digit

CC matches a control character

Pattern modifier (Pattern Modifiers):

Pattern modifiers are particularly used in ignoring case, matching multiple lines, and mastering this modifier can often solve many of the problems we encounter.

I-can match uppercase and lowercase letters

M-Treats a string as multiple lines

S-Treats the string as a single line, and the newline character is treated as ordinary characters, so that "." Match any character

X-whitespace in the pattern is ignored

U-Match to the nearest string

E-Use the replaced string as an expression

Format:/apple/i matches "Apple" or "apple" and so on, ignoring case. /I

Pcre Mode Unit:

/^d{2} ([W]) d{2}\1d{4}$ matches strings such as "12-31-2006", "09/27/1996", "86 01 4321". However, the above regular expression does not match the format of "12/34-5678". This is because the result "/" of the Pattern "[W]" has been stored. When the next position "1" is referenced, its matching pattern is also the character "/".

Use non-storage mode units when you do not need to store matching results "(? :)”

For example/(?: A|b|c) (d| e| F) \1g/will match "AEEg". In some regular expressions, it is necessary to use a non-storage mode unit. Otherwise, the order of subsequent references needs to be changed. The above example can also be written/(A|B|C) (c| e| F) 2g/.

Pcre Regular Expression functions:

 
  
  
  1. Preg_match () and Preg_match_all ()
  2. Preg_quote ()
  3. Preg_split ()
  4. Preg_grep ()

Function of the specific use, we can be found through the PHP manual, the following share some of the usual accumulation of regular expressions:

Match Action Property

 
  
  
  1. $str;
  2. $match;
  3. preg_match_all (/s+action= "(?! http:) (. *?) " s/$str$match);
  4. print_r ($match

Using callback functions in regular

 
 
  1. /**
  2. * Replace some string by callback function
  3. *
  4. */
  5. function Callback_replace () {
  6. $url = http://esfang.house.sina.com.cn ;
  7. $str = ;
  8. $str

    http://www.bkjia.com/PHPjc/486470.html www.bkjia.com true http://www.bkjia.com/PHPjc/486470.html techarticle PHP Regular expressions are primarily used for pattern segmentation, matching, finding, and substitution of strings. Using regular expressions can be inefficient in some simple environments, so how to better make ...

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.