Home > Backend Development > PHP Tutorial > A preliminary exploration of regular expressions in PHP (1) Basics_PHP Tutorial

A preliminary exploration of regular expressions in PHP (1) Basics_PHP Tutorial

WBOY
Release: 2016-07-13 09:55:59
Original
1032 people have browsed it

A preliminary exploration of regular expressions in PHP (1) Basics

Regular expressions are a difficult part for PHP beginners to face. Over time, they tend to forget the functions of various symbols. But after a systematic summary, you will find that the difficulty of memorization is not very difficult.

Perl is a common compatible regular expression function, the general form is (preg_), which is what this article mainly introduces.

Learning regular rules is nothing more than learning 1. Writing patterns
2. Regular function = powerful character processing function.


Let’s cover the basics first.
1. Atom, as you can tell from the name, is the smallest unit of a string. It includes two types:
① Printable characters are characters that generally have no special meaning.
②Unprintable characters, representative range:
d: represents any decimal number
D: represents any character other than a number
s: represents any blank character
S: represents any non-whitespace character
w:a-zA-Z0-9
W: Any character except a-zA-Z0-9
(Tips: These are usually the only ones. It’s easy to remember. Just remember that each one has its English abbreviation (d stands for digital, s stands for word, and s stands for space. The meaning is also very clear), and the capital letters are always Will fight with lower case)

2. Customized atom table ([])
[]Character class, matches any one of the atoms, a square bracket matches only one character
The opposite [^] excludes characters and excludes any character in square brackets
[-] represents a certain range, for example: [a-z] represents any character from a to z


Learn more about some common characters.

1. Delimiter (a symbol that makes sure it is a regular string rather than an ordinary string) (| / ...)
Definition: Any symbol other than letters, numbers, and forward slashes is a delimiter symbol. Generally, a backslash (/) is used to represent
Shape like: /tm/
2. Period character (.)
Can match any character except newline character.
A question that can explain this effect well is to write several words starting with s and ending with t.
/^s**t$/ /^s*t/ ……
3. Qualifier (?* {n,m})
? : Matches the previous character 0 or 1 times
: Matches the previous character 1 or more times
* : Matches the previous character 0 or more times
{n}: Match the previous character n times
{n,}: Match the previous character at least n times
{n,m}: Match the previous character at least n times and at most m times
4. Select the character (|)
For example: /cat|dog/ matches cat or dog
5. Bracket characters (())
() has many functions, including new functions produced when used together with functions
1. Increase priority
2. Used as a large atom
3. Used as the sub-mode (1 takes the first sub-mode, 2 takes the second mode...)
"\1" '1'

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/990600.htmlTechArticlePreliminary exploration of regular expressions in PHP (1) Basics Regular expressions are more difficult for PHP beginners to face As time goes by, the functions of various symbols are often forgotten. But after...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template