Introduction to PHP Regular Expressions _php tutorial

Source: Internet
Author: User
Today to see a tutorial has written some regular expression usage, which mainly refers to the regular character segmentation, matching, find, replace and some of the introductory knowledge and common examples, so organized to share with you.

1. Introduction and function of regular expressions.

01. What is a regular expression?

Regular Expressions (English: Regular expression, regex, or RegExp, abbreviated as RE), also translated as formal notation, conventional notation, in computer science, refers to a single string that describes or matches a series of strings that conform to a certain syntactic rule. In many text editors or other tools, regular expressions are often used to retrieve and/or replace text content that conforms to a pattern. Many programming languages support the use of regular expressions for string manipulation.

Rule syntax

02. Main role: Split, match, find, replace.

An expression Match

/^s*$/

Matches a blank line.

/d{2}-d{5}/

Validates an ID number consisting of two digits, a hyphen, plus 5 digits.

/ ]*)? >[ss]* /

Matches the HTML tag.


two commonly used regular functions in 2.PHP.

Preg_match the regular function, based on the Perl prophecy. (More efficient, you need to customize a start terminator.) )
Ereg regular functions, based on POSIX (Uniox, Script).

3. The elements included in the regular expression.

01. Atom (Normal character: A-Z A-Z 0-9, Atomic table, escape character).
02. Atomic character (characters with special functions).
03. Mode modifier (System built-in part module, similar function).


4. "Atom" in the regular expression.

01.a-z A-Z _ 0-9//The most common characters.
(ABC) (SKD)//the unit symbol enclosed in parentheses.
03.[ABCS] [^ABD]//greedy match, enclosed in square brackets from the table, the atomic table ^ represents the exclusion or opposite content.

04. Escape character (case sensitive)
D contains all numbers = = [0-9].
D does not contain all numbers = = [^0-9].

W contains all english characters = = [A-za-z_0-9].
W does not contain all English characters & numbers to match the special symbol = = [^a-za-z_0-9].
s contains blank areas such as carriage return, newline, page break = = [FNR].

Metacharacters

* 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, the boundary 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 into memory, can use 1 2 ... Get

Instance:

The code is as follows Copy Code

$mode = "#test #"; This can be matched with the above atomic table.
$str = "SDFSSTESTDF";

if (Preg_match ($mode, $str, $end)) {//mode regular module, str regular content, end regular result, output as array.
echo "Match succeeded". $end [0];
} else {
echo "Match failed";
}
?>

Common regular

* 1, ^s+[a-z a-z]$ can not be empty space can only be the English alphabet
* 2, S{6,} cannot be empty for more than six bits
* 3, ^d+$ cannot have a space cannot be not a number
* 4, (. *) (. Jpg|. BMP) $ can only be JPG and BMP format
* 5, ^d{4}-d{1,2}-d{1,2}$ can only be 2004-10-22 format
* 6, ^0$ Select at least one item
* 7, ^0{2,}$ Select at least two items
* 8, ^[s| s]{20,}$ cannot be empty for more than 20 words
* 9, ^+? [A-z0-9] (([-+.]| [_]+)? [a-z0-9]+] *@ ([a-z0-9]+ (. | -)) +[a-z]{2,6}$ Mail
* 10, w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) * ([,;] s*w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *) * Enter multiple addresses to separate messages with commas or spaces
* 11, ^ (([0-9]+))? [0-9] {7,8}$ Phone number 7-bit or 8-bit or preceded by an area code such as (022) 87341628
* 12, ^[a-z A-Z 0-9 _]+@[a-z A-Z 0-9 _]+ (. [ A-Z 0-9 _]+) + (, [A-Z-A-0-9 _]+@[a-z a-Z
0-9 _]+ (. [ A-Z 0-9 _]+) +) *$
* Can only be letters, numbers, underscores; must have @ and. format to standardize messages
* ^w+@w+ (. w+) + (, w+@w+ (. w+) +) *$ The above expression can also be written like this, more concise.
^w+ ((-w+) | (. w+)) *@w+ ((. | -) w+) *.w+$ [/size]
Regular expressions that match Chinese characters: [U4E00-U9FA5]


Match a specific number:

^[1-9]d*$//Match positive integer
^-[1-9]d*$//Match negative integer
^-? [1-9]d*$//Match integer
^[1-9]d* |0$//Match nonnegative integer (positive integer + 0)
^-[1-9]d* |0$//matches a non-positive integer (negative integer + 0)
^[1-9]d*.d* |0.d*[1-9]d*$//matching positive floating point number
^-([1-9]d*.d* |0.d*[1-9]d*) $//Match negative floating point number
^-? ([1-9]d*.d* |0.d*[1-9]d* | 0+ | $//Match floating point number
^[1-9]d*.d* |0.d*[1-9]d* | 0+ |0$//matching nonnegative floating-point number (positive floating-point number + 0)
^ (-([1-9]d*.d* |0.d*[1-9]d*)] | 0+ |0$//matching non-positive floating-point number (negative floating-point number + 0)

Consists of a letter a~z (case insensitive), a numeric 0~9, a minus sign, or an underscore
can only start and end with a number or letter the user name length is 4~18 characters

The code is as follows Copy Code

^[a-za-z0-9]{1}[a-za-z0-9|-|_]{2-16}[a-za-z0-9]{1}$

The user name is uppercase or lowercase letters or underscores and starts with a letter with a length of 6-20

The code is as follows Copy Code

^[A-ZA-Z][WD_]{5,19}

User name: including English lowercase, Chinese characters, numbers, underscores, not all numbers, underline cannot be at the end

The code is as follows Copy Code

/^[a-z0-9_u4e00-u9fa5]+[^_]$/g

Utf-8 under

Preg_match ("/^[a-z0-9_x80-xff]+[^_]$/g", $a);

GBK under:

Preg_match ("/^[a-z0-9_". Chr (0XA1). " -". Chr (0xff)." +[^_]$/", $a)

Mailbox

The code is as follows Copy Code

function Is_email ($email) {
return strlen ($email) > 6 && preg_match ("/^[w-.") +@[w-]+ (. w+) +$/", $email);
}
?>

URL address

The code is as follows Copy Code

function Autolink ($foo)
{
$foo = Eregi_replace (' ((f|ht) {1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+] ', '/1 ', $foo);
if (Strpos ($foo, "http") = = = = FALSE) {
$foo = Eregi_replace (' (www.[ -a-za-z0-9@:%_/+.~#?&//=]+) ', '/1 ', $foo);
}else{
$foo = Eregi_replace (' ([: Space:] () [{}]) (www.[ -a-za-z0-9@:%_/+.~#?&//=]+) ', '/1/2 ', $foo);
}
return $foo;
}
?>

http://www.bkjia.com/PHPjc/632636.html www.bkjia.com true http://www.bkjia.com/PHPjc/632636.html techarticle today to see a tutorial has written some regular expression usage, which mainly refers to the regular character segmentation, matching, find, replace and some of the introduction of knowledge and common examples, so the whole ...

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