PHP Learning Regular Expression Courseware 1th/2 page _php Basics

Source: Internet
Author: User
Tags ereg posix
Regular expressions

There are two sets of regular expression libraries in PHP that are similar in functionality, but have slightly different execution efficiencies:
A set is provided by the Pcre (Perl compatible Regular Expression) library. A function named after a prefix using "preg_";
A set of (PHP default) extensions provided by POSIX (Portable operating System Interface of Unix). Use a function named "Ereg_" as a prefix;
In PHP, regular expressions have three effects:
Match, and is often used to extract information from a string.
Replaces the matching text with the new text.
Splits a string into a smaller set of pieces of information.

Contains at least one atom in a regular expression.
Atoms (ordinary character, such as English characters)
Metacharacters (characters with special functions)
Pattern correction character (correction of regular expression semantics)


Atom (Atom)
A single character, number, such as a~z,a~z,0~9.
A pattern unit, such as (ABC), can be understood as a large atom composed of multiple atoms.
Atomic tables, such as [ABC].
A reusable mode unit, such as: \\1
Normal escape characters, such as: \d, \d, \w
Escape meta characters, such as: \*,\.

POSIX regular Expressions
POSIX regular expressions are all called portable operating system Interface of Unix, meaning that the UNIX portable operation system implements the interface.

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


Metacharacters (Meta-character)
A metacharacters is a character that is used to construct a regular expression with special meaning. If you want to include the metacharacters themselves in a regular expression, you must precede it with "\" to escape
Metacharacters description
* 0 times, 1 times or more to match the atoms before them
+ 1 or more times to match the atoms before it
? 0 times or 1 times to match the atoms before them
| Match two or more selection columns, such as [1-9]| [a-b]| [A-z] matches any of them as ture
^ matches the atoms of a string string first such as ABSCD===^AFDGFGF
$ matches the tail of a string of atoms such as dasdsv===v$
[] matches any atom in square brackets such as S===[dsadas]
[^] matches any character except the atom in square brackets such as aaaaa===[dddd]
{m} indicates that the former atom appears just as M times
{M,n} indicates that its front atom appears at least m times, at least N times (n>m)
{m,} indicates that its front atom appears not less than m times
() The whole represents an atom
. Matches any character other than a newline

^ $ These two original characters together are called the delimitation
Abd===^abc$ that's the only way to match.

Sequence of pattern matches
Sequential Meta-character description
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 function, Eregi () is the version of the ignored size of the ereg () function
Syntax format: if (!ereg (' ^[^./][^/]*$ ', $userfile))//mismatched format output die
{
Die (' This is an illegal filename! ');
}

Ereg_replace () and eregi_replace (ignore case) replace
String Eregi_replace ("Regular expression", "target substitution character", "replace Target")
Syntax format: $string = "This is a test";
Echo str_replace (' is ', ' was ', $string);
Echo ereg_replace ("() is", "\\1was", $string); \\1 to inherit the first whole
Echo Ereg_replace (() is), "\\2was", $string); \\2 inherits the second whole

Split () and Spliti (ignore case) splits a string into an array with a regular expression
List: Assigning variables to values in an array
Grammatical format: $date = "04/30/1973";
List ($month, $day, $year) = Split (' [/.-] ', $date);//list three variables corresponding format/split split in what form
echo "Month: $month; Day: $day; Year: $year <br/>\n ";
The output result month:04; day:30; year:1973
Current 1/2 page 12 Next read the full text

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.