PHP Regular Expression Learning (Appendix video tutorial) _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
PHP Regular Expression Learning (Appendix video tutorial ). PHP regular expressions are mainly used for pattern segmentation, matching, search, and replacement of strings. Regular expressions may be inefficient in some simple environments. therefore, how to make PHP regular expressions more efficient is mainly used for string mode segmentation, matching, search, and replacement. Regular expressions may be inefficient in some simple environments. Therefore, you need to consider how to better use PHP regular expressions.

My PHP regular expression entry is an article on the internet. This article explains the PHP regular expression method from a simple perspective. I think it is a good entry-level material, however, it is still dependent on the individual who will continue to forget it when using it. Therefore, I read this article four or five times repeatedly. for some of the difficult knowledge points, it may take a long time to digest, but as long as you can stick to it, you will find that your use of regular expressions will significantly improve.

BkJia video tutorial 36: learning and applying regular expressions in PHP (1)
BkJia video tutorial 37: learning and applying regular expressions in PHP (2)
BkJia video tutorial 38: learning and applying regular expressions in PHP (3)
BkJia video tutorial 39: learning and applying regular expressions in PHP (4)

PHP regular expression definition:

A syntax rule used to describe character arrangement and matching modes. It is mainly used for string mode segmentation, matching, search and replacement operations.

Regular functions in PHP:

PHP has two sets of regular functions, which have similar functions:

One set is provided by the PCRE (Perl Compatible Regular Expression) library. Functions with the prefix "prefix;

A set of extensions provided by POSIX (Portable Operating System Interface of Unix. Use a function named with the prefix "ereg _". (POSIX regular expression library is not recommended since PHP 5.3 and will be removed from PHP6)

Since POSIX regular expressions are coming soon, and the PCRE and perl forms are similar, it is more conducive to switching between perl and php, so here we will focus on the use of PCRE regular expressions.

PCRE regular expression

PCRE is called Perl Compatible Regular Expression, which means Perl is Compatible with Regular expressions.

In PCRE, a pattern expression (that is, a regular expression) is usually included between two backslash "/", such as "/apple /".

There are several important concepts in regular expressions: metacharacters, escaping, pattern units (duplicates), assignees, references, and assertions, these concepts can be easily understood and mastered in article [1.

Frequently used metacharacters (Meta-character ):

Metacharacters

A matches the atom at the beginning of A string.

Matches the atoms at the end of a string.

Match the boundary of a word/is/string with the matching header is/string with the matching tail is/boundary

B matches any character except word boundary/Bis/Matches "is" in the word "This"

D. match a number. it is equivalent to [0-9].

D. match any character except a number. it is equivalent to [^ 0-9].

W matches an English letter, number, or underline. it is equivalent to [0-9a-zA-Z _]

W matches any character except English letters, numbers, and underscores. it is equivalent to [^ 0-9a-zA-Z _].

S matches a blank character; equivalent to [fv]

S matches any character except the white space. it is equivalent to [^ fv]

F matching a page feed is equivalent to x0c or cL

Match a line break; equivalent to x0a or cJ

Matching a carriage return is equivalent to x0d or cM.

Match a tab. it is equivalent to x09 or \ cl.

V matches a vertical tab. it is equivalent to x0b or ck.

ONN matches an octal number.

XNN matches a hexadecimal number.

CC matches a control character

Pattern Modifiers ):

Pattern delimiters are used in case-insensitive and multi-row Matching. these delimiters often solve many problems.

I-matching uppercase and lowercase letters at the same time

M-treat strings as multiple rows

S-treats a string as a single line, and line breaks are treated as common characters so that "." matches any character.

The white space in X-mode is ignored.

U-match to the nearest string

E-use the replaced string as the expression

Format:/apple/I matches "apple" or "Apple", and case insensitive. /I

PCRE mode unit:

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

When you do not need to store matching results, use the non-storage mode Unit "(? :)"

For example /(? : A | B | c) (D | E | F) \ 1g/will match "aEEg ". In some regular expressions, it is necessary to use non-storage mode units. Otherwise, you need to change the subsequent reference sequence. The preceding example can also be written as/(a | B | c) (C | E | F) 2g /.

PCRE regular expression function:

 
 
  1. Preg_match () and preg_match_all ()
  2. Preg_quote ()
  3. Preg_split ()
  4. Preg_grep ()
  5. Preg_replace ()

For specific functions, we can find them in the PHP Manual. Below are some accumulated regular expressions:

Matching action attributes

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

Use callback in regular expressions

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

    Bytes. Regular expressions may be inefficient in some simple environments, so how to make it better...

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.