PHP Regular Expression usage Tips _php Tutorial

Source: Internet
Author: User
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 first atom of a string

/Z matches an atom at the end of a string

/b matches the boundary of the word//bis/match header is the string/is/b/matches the string//bis/b/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 [/f/t/v]

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

/F matches a page break equivalent to/x0c or/CL

Matches a newline character, equivalent to/x0a or/CJ

Match a return character equivalent to/x0d or/cm

/t matches a tab; 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:

1 extracting attributes of first bit

/^/d{2} ([/w])/d{2}//1/d{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:

The following is the referenced content:

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


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

The following is the referenced content:

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


Using callback functions in regular

The following is the referenced content:

/**
* 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;
}


Regular matching with assertions

The following is the referenced content:

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

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


Replace the address in the HTML source

The following is the referenced content:

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


Finally, while the regular tools are powerful, in terms of efficiency and writing time, it may not be possible to explode more directly, and for some urgent or demanding tasks, a simple, crude approach may be better.

http://www.bkjia.com/PHPjc/371448.html www.bkjia.com true http://www.bkjia.com/PHPjc/371448.html techarticle the definition of a regular expression for PHP: A syntax rule for describing character permutations and matching patterns. It is used primarily for pattern segmentation, matching, finding, and substitution of strings. PHP in the positive ...

  • 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.