Regular expression matching rules for PHP core technology and best practices

Source: Internet
Author: User

Regular expression matching rules for PHP core technology and best practices

This article describes several common matching rules.

1. Character groups

Finding numbers, letters, and blanks is simple because you already have metacharacters that correspond to these collections, but if you match a character set that doesn't have predefined metacharacters, it's simple to list them in square brackets.

For example: [aeiou] matches any one of the English vowel Letters, [. *?] Match one of the punctuation marks. Note that the metacharacters inside the square brackets lose their special meaning.

You can also specify a range of characters, for example [0-9] meaning and \d exactly the same: representing a single digit; [a-za-z0-9] is equivalent to \w;

Character groups are simple, but it's important to figure out when to escape a character group.

2. Escaping

If you want to find or match the meta-character itself, such as Find *,? There are problems: there is no way to specify them, because they will be interpreted as other meanings. It is necessary to remove the special meaning of these characters. This is called escaping.

Using a backslash (\) in PHP means escaping, and \q and \e can also omit the regular expression's metacharacters in the pattern. Like what:
\d +\q.$.\e$

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

3. Anti-righteousness

Sometimes, the characters you look for are not part of a character class, or the expression is the opposite of a well-known definition, such as a character other than a number, and you need to use the antisense.

Common Anti-righteousness:

Common anti-semantic

Describe

\w

Matches any character that is not a letter, number, underscore, or kanji

\s

Match any character that is not a whitespace character

\d

Match any non-numeric character

\b

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

[^x]

Matches any character except X

There is an obvious feature of Antisense, which is the opposite of some known meta-characters and is capitalized. For example, "\d" means non-numbers.

1) String that does not contain whitespace characters

\s+

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

<a[^>] +>

Tips:

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

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

Attention:

Do not arbitrarily use the antisense, because the anti-righteousness implicitly expands the scope, but makes oneself not to consider.

4. Branch

Branching is where there are multiple possible matches.

(c|h|f|to|) Cat

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

5. Grouping

Repeating a single character simply appends the qualifier to the character, but if you want to repeat multiple characters?

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 after exp

(?! Exp

Match the location that is not exp after

(? <!exp)

Match a location that is not previously exp

Comments

(? #comment)

Comments, which have no effect on the regular

Regular expression matching rules for PHP core technology and best practices

Related Article

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.