Home  >  Article  >  Backend Development  >  Detailed explanation of regular expression metacharacters and ordinary characters

Detailed explanation of regular expression metacharacters and ordinary characters

小云云
小云云Original
2018-01-20 09:34:332038browse

If you want to learn regular expressions, understanding metacharacters and ordinary characters is a difficulty that must be overcome. No need to remember it deliberately. The editor below has brought you knowledge about regular expression metacharacters and ordinary characters. Let’s take a look. I hope it can help you.

According to the regular expression grammar rules, the matching pattern of the regular expression is composed of a series of characters.

1. Ordinary characters:

Most characters can only describe themselves. These characters are called ordinary characters, such as all letters and numbers.

That is to say, ordinary characters can only match the same characters as them in the string.

2. Metacharacters:

Since ordinary characters can only match characters that are the same as themselves, the flexibility and powerful matching functions of regular expressions cannot be fully demonstrated, so regular expressions stipulates a series of special characters. These characters are not matched according to the direct quantity of characters, but have special semantics.

For example, the following characters:

^ $ . * + ? = ! : | \ / ( ) [ ] { }

Although the above Characters have special meanings, but some characters only have special meanings in certain contexts.

If you want to match these character literals with special meanings, you need to add a backslash (\) in front of these characters to escape. For example, if I want to match a $ literal, I need to write it as \$, otherwise it will match. An ending position. It is precisely because of the existence of these special characters that regular expressions have powerful functions.

Because they are the basic characters for constructing various regular expressions that match complex text, they are called metacharacters.

Related recommendations:

Introduction and application of regular metacharacters

PHP—PCRE regular expression metacharacters

Complete list of regular expression metacharacters and behavior description_PHP tutorial

The above is the detailed content of Detailed explanation of regular expression metacharacters and ordinary characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn