Introduction to PHP Regular expressions

Source: Internet
Author: User
Tags php language

Introduction to Regular expressions

The regular expression regular expression, which uses a single string to describe and match a series of strings that conform to a certain syntactic rule. The representative book "The way of the regular expression" is discussed in detail, this book for personal access to simplify, clear, easy to use, only the necessary concepts to do a supplementary explanation.

delimiting characters

The regular expression itself is a string that requires a set of qualifiers to distinguish what is the content of a regular expression. and # #和 {}, are qualifiers, used in the PHP language//As a qualifier for regular expressions. It is generally not recommended to use {} as a qualifier, because {} itself can also be a meta-character of a regular expression, easily confused.

Metacharacters

| Match two or more branch selections
[] matches any atom in square brackets
[^] matches any character outside of an atom in square brackets, equivalent to a non-
. Match any one character except for the line break
\d matches any decimal number, i.e. [0-9]
\d matches any non-decimal number, i.e. [^0-9]
\s matches any invisible atom (white space character), i.e. [\f\n\r\t\v]
\s matches any non-invisible atom (white space character), i.e. [^\f\n\t\v]
\w matches any number, letter, or underscore, i.e. [0-9a-za-z_]
\w matches any non-digit, letter, or underscore, i.e. [^0-9a-za-z_]

Quantifier (number of matches)

{n} The atom is exactly repeated n times before
{n,} front atom repeats >=n times
{n,m} before atom repeats greater than or equal to n times, less than or equal to M times
* Repeat 0 or more times
+ repeat 1 or more times
? repeat 0 or 1 times

Border control (match position)

() match the set of atoms in parentheses as an atom
\b Match the start or end position of a word
^ Start position of matching string
$ match the end position of the string
\g matches the end of the previous match (the start of this match)
\a matches the beginning of the string (similar to ^, but not affected by the processing of multiline options)
\z matches the end of a string, or end of line (unaffected by the multi-line option being processed)
\z matches the end of a string (similar to $, but not affected by the processing of multiline options)

Correction mode

Greedy match, the match result is ambiguous when it takes its length (default mode in PHP)
Lazy match, the match result existence ambiguity when take its short
U Fixed to lazy mode
I ignore the case of English letters
x ignore whitespace characters
S-let meta-character. Match any character including line break
E If this modifier is set, Preg_replace () makes a normal substitution of the inverse reference in the replacement string, evaluates it as a PHP code, and replaces the searched string with its result

Escape

The metacharacters in the regular are escaped by adding \:
$ ( ) * + . [ ] ? \ ^ { } |

Non-empty

.+

Mainland Mobile Number

1[34578]\D{9}

Email

^\w+ (. w+) *@\w+ (. w+) +$

Url

^ (https?:/ /)? (\w+.) +[a-za-z]+$

Introduction to 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.