Regular Expression Matching Rules for PHP core technologies and best practices

Source: Internet
Author: User
Regular Expression Matching Rules for PHP core technologies and best practices Regular Expression Matching Rules for PHP core technologies and best practices

This article describes several common matching rules.

1. Character Group

It is easy to search for numbers, letters, and spaces, because there are already metacharacters corresponding to these sets. However, if you do not match a character set that does not have predefined metacharacters, the method is simple, is to list them in square brackets.

For example, [aeiou] matches any English vowel, [. *?] Match one of the punctuation marks. Note that the metacharacters in square brackets do not have any special meaning.

You can also specify the character range, for example, the meaning of [0-9] is exactly the same as \ d: represents a digit; similarly, [a-zA-Z0-9] is equivalent to \ w;

Character groups are simple, but you must know when to escape character groups.

2. escape

If you want to find or match the metacharacters themselves, such as search *,? Wait for the problem to occur: you cannot specify it because it will be interpreted as something else. In this case, \ is needed to cancel the special meaning of these characters. This is called escape.

In PHP, backslash (\) is used to indicate escape. \ Q and \ E can also ignore the metacharacters of regular expressions in the mode. For example:
\ D + \ Q. $. \ E $

The above expression first matches one or more numbers, followed by A. point number, followed by a $, followed by A. point number, and finally the end of the string. That is to say, the metacharacters in \ Q and \ E are matched as common characters.

3. assense

Sometimes, the character to be searched does not belong to a character class, or the expression is opposite to a known definition (for example, a character other than a number). In this case, it is necessary to use a negative sense.

Commonly used assignees:

Commonly used assignees

Description

\ W

Match any character that is not a letter, number, underline, or Chinese character

\ S

Match any character that is not a blank character

\ D

Match any non-numeric characters

\ B

Match is not the start or end of a word

[^ X]

Match any character except x

There is an obvious feature of assignees, that is, it is opposite to some known metacharacters and is in the upper case. For example, "\ D" indicates a non-number.

1) strings that do not contain blank characters

\ S +

2) string that is expanded with angle brackets and starts with:

] +>

Tip:

"^" Indicates no, not the beginning. How to differentiate?

^ At the beginning of a regular expression can only be used at the front end of a regular expression, and ^ at the back end can only be used in character groups, that is, it only appears in brackets.

Note:

Do not arbitrarily use the negative sense, because the negative sense virtually expands the scope, so that they did not take into account.

4. Branch

There are multiple possible matching conditions for branches.

(C | h | f | to |) cat

The expressions in the brackets are regarded as a whole. the branch condition refers to several rules that can be matched no matter which rule is satisfied, the specific method is to use the "|" method to separate different rules.

5. group

To repeat a single character, you only need to add a qualifier after the character, but if you want to repeat multiple characters?

Common grouping syntax:

Category

Syntax

Description

Capture

(Exp)

Match exp and capture text to automatically named group

(? Exp)

Match exp and capture the text to the name group

(? : Exp)

Matches exp and does not capture matched text

Assertion with zero width

(? = Exp)

Match the position before exp

(? <= Exp)

Match position after exp

(?! Exp)

Matching is not followed by exp

(?

Match the position that is not exp

Note

(? # Comment)

Does not affect the regular expression.

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.