Regular expression matching rules for PHP core technology and best practices ____php

Source: Internet
Author: User

regular expression matching rules for PHP core technology and best practices

This article introduces several common matching rules.

1. Character Group

Finding numbers, letters, and whitespace is simple because you already have the metacharacters that correspond to these collections, but if you match a character set that does not have predefined metacharacters, the method is simple enough to list them in square brackets.

For example: [AEIOU] matches any one English vowel letter, [. *?] Matches one of the punctuation. Note that the metacharacters in square brackets lose their special significance at this point.

You can also specify a range of characters, such as [0-9], which is exactly the same as the \d: represents a digit, and the same [a-za-z0-9] equals \w;

Character groups are simple, but be sure to figure out when to escape in character groups.

2. Escape

If you want to find or match the meta characters themselves, such as finding *,. There is a problem: there is no way to specify, because they will be interpreted as another meaning. Then you need \ To remove the special meaning of these characters. This is called escape.

Using backslashes (\) in PHP to represent escape, \q and \e can also ignore the metacharacters of regular expressions in the pattern. Like what:
\d +\q.$.\e$

The expression above matches one or more digits, followed by a. Dot number, then a $, and then one. The dot number, and finally the end of the string. That is, the metacharacters in \q and \e are matched as normal characters.

3. Anti-righteousness

In some cases, the lookup character does not belong to a character class, or the expression is contrary to a known definition, such as a character other than a number, and then a justification is needed.

Common Anti-righteousness:

Common anti-righteousness

Describe

\w

Match any characters that are not letters, numbers, underscores, Chinese characters

\s

Match any character that is not a blank symbol

\d

Match any number of non-numeric characters

\b

Matches a position that is not the beginning or end of a word

[^x]

Matches any character other than X

Anti-Righteousness has a more obvious feature, which is the opposite of some known metacharacters and is uppercase. For example, "\d" means non-numeric.

1 A string that does not contain whitespace characters

\s+

2) A string that expands with angle brackets and begins with a:

<a[^>] +>

Tip:

"^" Here is the meaning of right and wrong, not the beginning. How to differentiate.

The ^ at the beginning can only be used at the very front of the regular expression, and the ^ ^ ^ can only be used in the character group, that is, only within the brackets.

Note:

Do not arbitrarily use antisense, because the antisense is to expand the scope, but not to take into account.

4. Branch

Branching is a variety of possible matching scenarios.

(c|h|f|to|) Cat

The expressions in parentheses are treated as a whole, and the branching condition refers to several rules that can be matched regardless of the rule, by using the "|" Method separates the different rules.

5. Grouping

Repeating a single character only requires a qualifier directly after the character, but if you want to repeat more than one character.

Common grouping Syntax:

Category

Grammar

Describe

Capture

(exp)

Match exp, and capture text into an automatically named group

(? <name>exp)

Match exp, and capture the text into the group of name

(?: EXP)

Match exp, do not capture matching text

0 Wide Assertion

(? =exp)

Match the position of the exp front

(? <=exp)

Match the position of the exp back

(?! Exp

Matches a position that is not followed by exp

(? <!exp)

Matches a position that is not exp at the front

Comments

(? #comment)

Comments, no positive effects

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.