Regular expressions for PHP development (1)
Speaking of regular expressions, I encountered them when I was learning compilation principles in college. I didn’t pay much attention to them at that time. I just understood a little bit. Later when I was making an app, I learned that this is just a point that is actually very important, no matter it is It is used in many places on websites and apps. In the past, every time I used it, I looked for ready-made ones on the Internet, but it was very painful to not be able to write regular expressions that met the requirements myself. Start learning below.
1. Row locators (^ and $)
Row locators are convenient for describing strings. "^" represents the beginning of the line, "$" represents the end of the line
For example: ^Jack represents a string starting with Jack, then "Jack is a hero" can match, but "The super man is Jack" cannot match
For example: Jack$ represents a string ending with Jack, then "The super man is Jack" can match
2. Word delimiter (b, B)
If you want to match a complete word instead of a word part, then you need to use word delimiters
For example: bworkb means that the word work is in the string Then "I'm work hard!" can match
For example: BworkB means that the word work cannot be included in the string Then "I'm work hard!" cannot match
3. Character class ([])
Regular expressions are case-sensitive. If you want to ignore case, you can use the square bracket expression "[]". As long as the matching word appears within square brackets, the match is successful. But please note that a square bracket can only match one character. For example, if you want to match a string ab that is not case-sensitive, the expression should be as follows: [Aa][Bb] This way you can match all the ways of writing ab
4. Select the character (|)
There is another way to achieve matching A string ab is not case-sensitive, that is, the selection character (|) is used. This character can be understood as "or", then the expression is: (A|a)(B|b)
5. Hyphen (-)
The naming rule of variables can only start with a letter and an underscore. But in this way, if you want to use a regular expression to match the first letter of a variable name, you have to write it as [a,b,c,d...A,B,C,D...] This is undoubtedly very troublesome Yes, the regular expression provides a hyphen "-" to solve this problem. A hyphen can represent a range of characters. For example, the above example can be written as [a-zA-Z]
6. Exclusion character ([^])
'^' represents the beginning of the line, and placing this character in square brackets means exclusion. For example: [^a-zA-Z] This expression matches variable names that do not start with letters or underscores.
7. Qualifier (? * + {n,m})
For repeated letters or strings, you can use qualifiers to achieve matching. There are 6 main types of qualifiers, as shown in the following table:
8. Dot character (.)
The dot character (.) can match any character except the newline character. Note: Any character other than a newline character
If it matches a word that starts with a and ends with b and contains a character in the middle. Then the format is as follows: ^a.b$
9. Escape characters ()
The escape characters in regular expressions are similar to the escape characters in php. They are special characters (such as: "." "?" " ") into ordinary characters. Take the IP address 127.0.0.1 as an example. If no escape characters are used in this format, the expression result of the regular expression is as follows: [1-9]{1,3}(.[1-9]{1,3}) {3} But this is obviously wrong, because "." can be matched with any character. This means not only IPs like 127.0.0.1 will appear, but also letters and other characters. This is obviously wrong. At this time, the escape character will be sent. Come in handy, please escape characters. The regular expression using escape characters is [1-9]{1,3}(.[1-9]{1,3}){3}
10. Backslash()
(1) Backslash can convert some unprintable characters It is displayed as shown in the following table:
(2) Backslash can also specify a predefined character set as shown in the following table:
(3) Backslash can also define assertions as shown in the following table Shown:
11. Backreference
Backreference is to use the "memory" function of subexpressions to match consecutive strings or letters.
For example: If you want to match two consecutive ab, you can use ab as a group and then add "1" at the end. As follows: (ab)1
If the string to be matched is not fixed, you can write the string in brackets as a regular expression. If there are multiple groups, you can use "1", "2" to represent each group (the order is from left to Right)
For example: ([a-z])(A-Z)12
In addition to using numbers to represent groups, you can also specify the group name yourself, such as:
(?P
If To reference this group, the syntax is as follows:
(?P = subname)
Let’s rewrite the expression ([a-z])(A-Z)12. Name these two groups respectively and back-reference them. Regular expression The formula is as follows:
(?P
Let’s learn the theoretical knowledge together here, more More operations will be shared with you in Regular Expressions for PHP Development (2)
The above has introduced regular expressions in PHP development (1), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
