PHP learning Regular Expression courseware page 1/2

Source: Internet
Author: User
Tags ereg

Regular Expression

There are two sets of regular expression function libraries in PHP. The two functions are similar, but the execution efficiency is slightly different:
One set is provided by the PCRE (Perl Compatible Regular Expression) Library. Functions with the prefix "prefix;
A set of (default PHP) extensions provided by POSIX (Portable Operating System Interface of UNIX ). Use a function prefixed with "ereg;
In PHP, regular expressions have three functions:
Matching is often used to extract information from strings.
Replace the match text with the new text.
Splits a string into a group of smaller information blocks.

A regular expression contains at least one atom.
Atom (common characters, such as English characters)
Metacharacters (special characters)
Pattern correction characters (Regular Expression semantic correction)

Atom)
A single character or number, such as ~ Z, ~ Z, 0 ~ 9.
A pattern unit, such as ABC, can be understood as a large atom composed of multiple atoms.
Atomic table, such as [ABC].
The Mode unit used again, for example, \ 1.
Common escape characters, such as \ D, \ D, \ W
Escape metacharacters, for example :\*,\.

POSIX Regular Expression
POSIX regular expressions are all called portable operating system interface of UNIX, meaning UNIX portable operation system implementation interface.

The method for constructing a POSIX regular expression is the same as that for creating a mathematical expression, that is, using a variety of metacharacters and operators to combine small expressions to create a larger expression.

Metacharacters (meta-character)
Metacharacters are special characters used to construct rule expressions. To include metacharacters in a regular expression, you must add "\" before it to escape it.
Metacharacters
* Matches the previous atom 0 times, once, or multiple times
+ Match the previous atom once or multiple times
? Matches the previous atom zero or one time
| Match two or more select columns such as [1-9] | [A-B] | [A-Z] matches with any of them to true
^ Matches the atoms at the beginning of a string, for example, abscd ==^
$ Match the atoms at the end of a string, for example, dasdsv === v $
[] Any atom in the matching square brackets, for example, S === [dsadas]
[^] Match any character except the atom in square brackets, for example, AAAAA === [dddd]
{M} indicates that the first atom exactly appears m times.
{M, n} indicates that the first atom appears at least m times and at least N times (n> m)
{M,} indicates that the first atom appears no less than m times.
() Indicates an atom.
. Match any character except line breaks

^ $ The two original characters are collectively referred to as the delimiter.
Abd = ^ ABC $ only matches

Order of pattern matching
Sequence metacharacters
1 () mode Unit
2? * + {} Duplicate match
3 ^ $ Boundary Limit
4 | mode selection

POSIX Regular Expression Functions
Ereg () and eregi ()
Ereg_replace () and eregi_replace ()
Split () and spliti ()

Ereg () and eregi () ereg () string matching functions, eregi () is the version of the ereg () function that ignores the size
Syntax format: If (! Ereg ('^ [^./] [^/] * $', $ userfile) // output die in mismatched format
{
Die ('This is an invalid file name! ');
}

Replace ereg_replace () and eregi_replace (case-insensitive)
String eregi_replace ("Regular Expression", "target replacement character", and "target replacement ")
Syntax format: $ string = "this is a test ";
Echo str_replace ("is", "was", $ string );
Echo ereg_replace ("() is", "\ 1was", $ string); \ 1 is to inherit the first whole
Echo ereg_replace ("() is)", "\ 2was", $ string); \ 2 inherits the second whole

Split () and spliti (case-insensitive) use regular expressions to split strings into Arrays
List: assign some variables to values in the array.
Syntax format: $ date = "04/30/1973 ";
List ($ month, $ day, $ year) = Split ('[/. -] ', $ date); // lists the formats of the three variables. // the format in which the variables are split.
Echo "month: $ month; Day: $ day; year: $ year <br/> \ n ";
Output result month: 04; Day: 30; Year: 1973

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.