"Tamping PHP Basic series" PHP Regular expressions

Source: Internet
Author: User
Tags preg

I. Overview

1. Test of regular expression address: http://tool.chinaz.com/regex/

Second, the specific common regular expression

1. e-mail Regular expression:
1) \w[-\w.+]*@ ([a-za-z0-9][-a-za-z0-9]+\.) +[A-ZA-Z]{2,14}
2)\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
3) Analysis:

-- "\" is called "Skip character". Used to escape Some special symbols, such as ".", "/"

--"\w" is equivalent to [a-za-z0-9_]. Also matches Chinese characters

   --"+" 1 to several times can also be written as {1}

-- parentheses to specify sub-expressions (also called groupings ), then you can specify the number of repetitions of this subexpression ([-+.] \w+)*

-- [-+.] The square brackets Expand the part is the Word value field .

-- . Match any character other than line break

4) Code example

  

<? PHP   $email = ' [email protected] ';   $preg = '/\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */'  ; $res Preg_match ($preg$email$preg _res);   Var_dump (111,$res,$preg _res); exit;

-- Preg_match(): http://php.net/manual/zh/function.preg-match.php

2. URL expression:

1) [a-za-z]+://[^\s]+

2) Analysis:

-- ^

> in Word Value field (e.g. [^\w]) for negation (not included)--"Reverse selection"

> precedes the expression, indicating that it starts with the current character. (/^n/i, which means start with N).

--\s matches any whitespace character

Third, appendix

1. Common meta characters
Code description
. Match any character other than line break
\w match letters or numbers or underscores
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string


2. Common Qualifiers
Code/Syntax Description
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times


3. Common antonyms
Code/Syntax Description
\w matches any characters that are not letters, numbers, underscores, kanji
\s matches any character that is not a white letter
\d matches any non-numeric character
\b Match is not where the word starts or ends
[^x] matches any character except X
[^aeiou] matches any character except the letters AEIOU

"Tamping PHP Basic series" 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.