Learn Small notes---regular expressions

Source: Internet
Author: User

Definition: A string of a certain format

Effect: 1. Used to determine whether a string meets certain requirements

2. You can go to a string and take out some strings that match the rules.

Format: consists of character family qualifier locators

Character family: A set of characters consisting of characters

[0-9] Number

[^0-9] Non-digital

[A-z] lowercase English

[A-z] capital English

[A-z] case in English

[A-za-z]

[a-za-z0-9] uppercase and lowercase English numerals

\d Digital

\w Digital English alphabet and underline

\d Non-digital

\w non-alphanumeric English letters and underscores

. Represents all (any) characters, except line breaks

Description: Special characters appear in the character cluster using \ \-escape

Qualifier: The number of occurrences of a character in a restricted character cluster

{m,n} appears m times to n times n must be greater than M

{3}3 bit 3 times

{m,}m times or more

{0,n}n times below

? 0 or once {0,1}

+ More than once {1,}

* More than 0 times {0}

Locator: ^ Start of string

$ End of string

\b The beginning of a word

\d start and end of non-word

| Or operation priority low first left then right

() sub-expression can be given the highest precedence of a qualifier expression match a position in a string

Match pattern: I is case-insensitive

S. Contains line breaks

Write the regular expression:

"/Regular Expression/"

"#正则表达式 #"

"! Regular Expression!"

$str = "fdafd565645fd56fd456afd465a";

"/[0-9]{1,}/"

matching process: From the left side of the string followed by the character cluster, if the counter plus 1 is met, the counter is already up to the maximum value. or it encounters a character that does not match the character cluster and stops. It then determines if the values in the counter match the qualifier requirements, and the consistency is matched, otherwise it continues.

To run an expression:

0|1 Preg_match_all (regular expression, string, array (returns an array of matching contents))

Match all

0|1 Preg_match (regular expression, string, array (returns an array of matching contents))

High efficiency match to stop once

Preg_split (regular expression, string, array (returns the matched content to an array))

Splitting a string with a regular expression

Preg_replace (regular expression, replaced by what, substituted string)

Use regular expressions to replace some substrings in a string

Reverse Reference: For the time being, it's more complicated and useful . Replace a string with a regular expression

Description: The qualifier and the character cluster can be repeated, must appear in pairs

And the use of:

Match 6-bit 3-9 digits.

/[3-9]{6}/

Match zip Code

/[0-9]{6}//\d{6}/

Match an An A

/a/

Match phone number

1 Opening

Second place: 345678

Third place and later: 0-9

"/[1]{1}[3-8]{1}[0-9]{9}/" cell phone good regular match expression

Preg_match_all ("/[1]{1}[345678]{1}[0-9]{9}/", "183888888888", $arr);

subexpression: A part of an expression is enclosed in parentheses, and a portion of it is more than a sub-expression, followed by a qualifier

Description: A subexpression can be followed with the use of a restriction character


Or the use of:

Matches a path string (contains. gif. jpg. png)

"/x|y|z/" each or both as a whole

^: Start position

"/^xyz/" regular expression starting with XYZ

$: End Position

"/xyz$/" regular expression ending with XYZ

"/^xyz$/" the regular expression starting with XYZ and ending with XYZ can only be XYZ

Regular Expressions for mailboxes

"/[a-za-z0-9_\-]{2,}@[0-9a-za-z\-]+ (\.[ a-za-z]+) +$/"

"/[a-z0-9_\-]{2,}@[0-9a-z\-]+ (\.[ A-z]{2,}) +$/i "Case insensitive

"/[\w][email protected]\w+ (\.\w) +$/"

An integer expression

"/^ ([\+\-]{1}[1-9]{1}) | ([1-9]{1})) \d*$/"

Positive integer expression

"/^ (\+[1-9]\d*) | ([1-9]\d*) $/"

"/^(\+? [1-9]\d*) $/"

An expression of a negative integer

"/^(\-? [1-9]\d*) $/"

Floating-point expression

/^[\+\-]? (0| [1-9]\d*] \.\d+$/

"/^[\+|\-]? (0| [1-9]\d*) \.\d+$/"

Positive floating-point expression

"/^ (\+0\.\d+) | (+[1-9]\.\d*) | (0\.\d+) | ([1-9]\d*\.\d+) $/"

"/^ (\+?0\.\d+) | (+? [1-9]\d*\.\d+) $/"

Negative floating-point expression

"/^\-? (0| [1-9]\d*) \.\d+$/"

Get all the contents of all P tags

"/(<p.*?>) (. *?) <\/P>/S "


To remove. * Greed:

.*?

Splitting a string by a number

$str = "FDADFSA12FDAFDA12FDAS1";

Preg_split ("/\d+/", $str, $arr);


This article is from the "Long Gray Corner" blog, please be sure to keep this source http://bamilk.blog.51cto.com/10785704/1702530

Learn Small notes---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.