PHP Regular Expression Summary

Source: Internet
Author: User

You can use a character as a wildcard to replace any character other than the line break (\ n. For example, a regular expression:
.
It can be matched with "cat", "sat", "# at", and "mat. It is mainly used when searching for files.

You can also specify a character set:
[A-z]
The content in square brackets ([]) is a character class-a combination of character sets to which matching characters belong. However, only one character is matched.

We can also provide a set.
[Aeiou]
It can be used to represent vowels.

It can also be used to indicate a range. It can also be a range set:
[A-zA-Z]
This range set represents any size letter.

In addition, a set can be used to indicate that the character does not belong to a specific set. For example:
[^ A-z]
It can be used to match any character not between a and z. If you include the Escape Character (^) in square brackets, it indicates no.

Repeated times
"*" Indicates that this mode can be repeated 0 times or more times. "+" indicates that this mode can be repeated once or more times.

[[: Alnum:] +
It indicates at least one letter.

Subexpression
(Very) * large
It can match very large, large, very large, and so on.

Subexpression count
You can use a numeric expression in curly braces ({}) to specify the number of times the content can be repeated. You can specify an exact number ({3} indicates repeated 3 times) or a range of repeated times.

({2, 4} indicates repeated 2 ~ 4 times), or an open-ended repetition range ({2,} indicates at least two times ).
For example:
(Very) {1, 3}
Match "very", "very", and "very ".

Locate the start or end of the string
The delimiters (^) are used to start a regular expression, indicating that the substring must appear at the start of the string to be searched. The character "$" is used at the end of the regular expression. The sub-string is required.

Must appear at the end of the string.

For example, match bob at the beginning of the string:
^ Bob

This mode will match the string that com appears at the end of the string:
Com $

Finally, this pattern matches strings that only contain one character from a to z:
^ [A-z] $

Branch

You can use a vertical line in the regular expression to represent a choice. For example, if you want to match com, edu, or net, you can use the following expressions:
Com | edu | net

Match special characters

To match special characters such as., {, or $, you must add a backslash (\) before them (\). To match a backslash, you must use two backslash.

In PHP, the regular expression mode must be included in a single quotation mark string.

In POSIX regular expressions, used to abstract special characters outside square brackets
Character meaning
\ Conversion character) submode end
^ Starts matching strings * repeated 0 or more times
$ Match + repeat once or more times at the end of the string
. Match the characters except the line break (\ n) {start of the minimum/maximum number of marks
| Select the START (OR) of the branch.} The End of the minimum/maximum number of marks.
(Start of the sub-mode? Mark a sub-mode as optional

In POSIX regular expressions, special characters in square brackets are used.

Character meaning
\ Conversion character
^ Non, used only at the start position
-Used to specify the character range

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.