Tips for using PHP regular expressions

Source: Internet
Author: User
Tags modifier php regular expression posix regular expression

The definition of the PHP regular expression:

A syntax rule used to describe the character arrangement and matching pattern. It is mainly used for pattern segmentation, matching, lookup and substitution operations of strings.

Regular functions in PHP:

PHP has two sets of regular functions, the two functions are similar, respectively:

A set is provided by the Pcre (Perl compatible Regular Expression) library. A function named after a prefix using "preg_";

One set is provided by the POSIX (portable operating System Interface of Unix) extension. Use a function named "Ereg_" (the POSIX regular function library, which is not recommended after PHP 5.3, will be removed after PHP6)

Because POSIX is about to launch the history stage, and Pcre and Perl form the same, more conducive to our transition between Perl and PHP, so here focus on pcre regular use.

Pcre Regular Expression

Pcre is all called Perl compatible Regular Expression, meaning 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 (repetition), antisense, references, and assertions, which can be easily understood and mastered in article [1].

Commonly used metacharacters (Meta-character):

Metacharacters description

/A matches the first atom of string strings

/z The atoms that match the tail of string strings

/b matches the bounds of the word//bis/the string/is/b/matching the header is the string that matches the end is the//bis/b/delimitation

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

/d matches a number; equivalent to [0-9]

/d matches any one character except the number; equivalent to [^0-9]

/w match 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 blank character; equivalent to [/f/t/v]

/S matches any one character except the white space character; equivalent to [^/f/t/v]

/F matches a page feed character equivalent to/x0c or/CL

Match a newline character; equivalent to/x0a or/CJ

Matching a return character is equivalent to/x0d or/cm

/t matches a tab character; equivalent to/x09/or/CL

/V matches a vertical tab; equivalent to/x0b or/ck

/onn match a octal number

/XNN matches a hexadecimal digit

/CC matches a control character

mode modifier (pattern modifiers):

The pattern modifier is especially used in ignoring case and matching multiple lines, and mastering this modifier often solves many of the problems we encounter.

I-can match uppercase and lowercase letters at the same time

M-Treat 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 negligible in pattern

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:

1 extracting first-bit attributes

/^/d{2} ([/w])/d{2}//1/d{4}$ matches strings such as "12-31-2006", "09/27/1996", "86 01 4321". However, these regular expressions do not match the format of "12/34-5678". This is because the result "/" of the Mode "[/w]" has been stored. The match pattern for the next position "/1" reference is also the character "/".

Use a non-storage mode unit 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 function:

The following are the referenced contents:

Preg_match () and Preg_match_all ()
Preg_quote ()
Preg_split ()
Preg_grep ()
Preg_replace ()


The specific use of the function, we can find through the PHP manual, the following share some of the regular expressions accumulated:

Match Action Property

The following are the referenced contents:

$str = ';
$match = ';
Preg_match_all ('//s+action=/') (?!) http:) (. *?) /"/s/', $str, $match);
Print_r ($match);


Using callback functions in regular

The following are the referenced contents:

/**
* Replace some string by callback function
*
*/
function Callback_replace () {
$url = ' http://esfang.house.sina.com.cn ';
$str = ';
$str = Preg_replace ('/<=/saction=/') (?!) http:) (. *?) (? =/"/s)/e ', ' Search (/$url,//1) ', $str);

Echo $str;
}

function Search ($url, $match) {
Return $url. '/' . $match;
}


A regular match with assertions

The following are the referenced contents:

$match = ';
$str = ' xxxxxx.com.cn bold font
Paragraph text

';
Preg_match_all ('/(?<=< (/w{1}) >). * (?=<///1>)/', $STR, $match);
echo "matches the contents of an HTML tag without attributes:";
Print_r ($match);


Replace the address in the HTML source code

The following are the referenced contents:

$form _html = Preg_replace ('/(? <=/saction=/"|/ssrc=/" |/shref=/") (?! Http:|javascript) (. *?) (? =/"/s)/e ', ' add_url (/$url,/'//1/') ', $form _html);


Finally, the regular tools are powerful, but in terms of efficiency and writing time, there may be no explode to be more direct, and for some urgent or demanding tasks, simple, rough methods may be 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.