PHP Regular Expressions

Source: Internet
Author: User
Tags ereg explode

what is a regular expression when writing a program or Web page that handles strings, there is often a need to find strings that match certain complex rules. Regular expressions are The syntax used to describe these rules. Example: In judging the user's email address format, mobile phone number format, or collect other people's Web content The main functions are: Split, match, find, replace two common regular functions in PHP 1.preg_match Regular functions, based on the Perl language Preg_match ( mode, string subject, array matches)   2.ereg regular function to POSIX basis (Unix, Script) ereg ( mode, string subject, array regs) elements included in a regular expression (1), Atom (normal character: A-Z-A-Z 0-9, Atomic table, escape character) (2), metacharacters (characters with special function) (3), mode modifier (System built-in part of the characters I, M, S, U ... ) "atoms" in regular expressions ①a-z A-Z _ 0-9//most common characters ② (ABC) (SKD)//the unit enclosed in parentheses conforms to ③[abcs] [^ABD]//The atomic table enclosed in square brackets, the ^ in the atomic table represents the excluded or opposite content ④ Escape Character \d contains all numbers [0-9] \d except all numbers [^0-9] \w contains all English characters [a-za-z_0-9] \w except for all English characters [^a-za-z_0-9]
\s contains blank areas such as carriage return, line wrapping, paging, etc. [\f\n\r]
...   Regular expression meta-characters   * Match the previous content 0 times 1 or more times . Matches content 0 times 1 times or more, but does not include carriage return line breaks + match 1 or more times of previous content ? Match the previous content 0 or 1 times | Select match like in PHP | (because this operation conforms to the weak type leading to the most overall match above) ^ Match String header contents $ match String trailing content \b Matches the word boundary, which can be a space or a special match \b Match exception with word boundary unexpected content {m} matches the previous content with a repeat number of M times {m,} The number of repetitions of the previous content is greater than or equal to M times {M,n} matches the number of repetitions of the previous content m times to n times () Merge the whole match, and put in memory, can use \1 \2 ... Get     1. Sequence of Operations still follow the arithmetic rules from left to right Priority Level   () parentheses because it is memory processing, so the highest *? + {} Repeat match content next ^ $ \b Border Processing third | Conditional handling Fourth Finally, the matching is calculated in order of operation   2. Mode modifier   a pattern modifier is a feature that is enhanced and supplemented for regular expressions and is used outside of regular Common Modifiers I regular content is not case-sensitive at match time (by default it is differentiated) m uses multi-line recognition matching when matching first content or tail content S will escape the carriage return cancellation is for a single line match as. x ignores whitespace in the regular A Force A match from the beginning D Force $ match tail without any content \ n U Disallow greedy match to trace only to the nearest match and end, regular expressions commonly used in acquisition programs  The application of regular expressions in PHP 1. Matching function 2. Replacement function 3. Split function 1. Matching function   Preg_match_all all matching functions Preg_match_all (string pattern, string subject, array matches [, int flags])   sorting results to $matches [0] An array that matches all patterns   Purpose: Intercept more detailed content, collect Web pages, analyze text 2. Replacement function preg_replace Regular Replacement function preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])   replace the relevant content with a regular expression, similar to the previously learned str_replace string substitution, but the function is stronger than it. Hint: 1, the replacement content can be a regular or an array of regular 2, the replacement content can be replaced by the modifier e to resolve the content of the execution   Purpose: Replace some of the more complex content, also can be used for the conversion of content   3. Split function Preg_split Regular Cutting Preg_split (string pattern, string subject [, int limit [, int flags]]) using regular expressions to cut related content, similar to the previously learned explode cutting function, but explode can only be cut in one way with limitations.  

PHP Regular Expressions

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.