Regular expression any character PHP learning regular expression Courseware 1th/2 page

Source: Internet
Author: User
Tags ereg posix
Regular expressions
There are two sets of regular expression libraries in PHP, which are functionally similar, but with slightly different execution efficiency:
The set is provided by the Pcre (Perl Compatible Regular Expression) library. A function named with the prefix "Preg_";
A set provided by the POSIX (portable Operating System Interface of Unix) extension (php default). Use a function named "Ereg_" as a prefix;
In PHP, regular expressions have three functions:
and is often used to extract information from a string.
Replaces matching text with new text.
Splits a string into a smaller set of information blocks.
Contains at least one atom in a regular expression.
Atoms (ordinary characters, such as English letters)
Metacharacters (characters with special function)
Pattern modifier characters (corrections to the semantics of regular expressions)
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 consisting of multiple atoms.
An atomic table, such as [ABC].
Re-use of the 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, which means the interface is implemented for the UNIX portable operation system.
The method of constructing a POSIX regular expression is the same as creating a mathematical expression, that is, using a variety of metacharacters and operators to combine small expressions together to create larger expressions.
Metacharacters (Meta-character)
Metacharacters is a special-meaning character used to construct a regular expression. If you want to include the meta-character itself in a regular expression, you must precede it with "\" to escape
Metacharacters description
* 0, 1 or more times to match the previous atom
+ 1 or more times to match the atoms before it
? 0 or 1 times to match its former atoms
| Match two or more selection columns such as [1-9]| [a-b]| [A-z] matches any of the ture
^ matches the first atom of a string such as ABSCD===^AFDGFGF
$ matches a string of strings at the end of an atom such as dasdsv===v$
[] matches any atom in square brackets such as S===[dsadas]
[^] matches any character except for atoms in square brackets such as aaaaa===[dddd]
{m} indicates that its former atom appears exactly m times
{M,n} indicates that its former atom appears at least m times, at least N times (n>m)
{m,} indicates that its former atom appears not less than m times
() whole represents an atom
. Match any one character except for line break
^ $ These two primitive characters together are called delimitation
That's the only way to match abd===^abc$.
Order of pattern matching
Sequential Meta-character description
1 () mode unit
2? * +{} repeat 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 ignored size version of the Ereg () function
Syntax format: if (!ereg (' ^[^./][^/]*$ ', $userfile))//mismatched format output die
{
Die (' This is an illegal filename! ');
}
Ereg_replace () and eregi_replace (ignoring 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 inherit the second whole
Split () and Spliti (ignoring case) use regular expressions to split a string into an array
List: Assigns some variables to the values in the array
Syntax format: $date = "04/30/1973";
List ($month, $day, $year) = Split (' [/.-] ', $date);//list three variables corresponding format//In what form split divide who
echo "Month: $month; Day: $day; Year: $year
\ n ";
The output result is month:04; day:30; year:1973

Current 1/2 Page 12 next page

The above describes the regular expression of any character PHP learning regular expression Courseware 1th/2 page, including the regular expression of any character content, I hope to be interested in PHP tutorial friends helpful.

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