


Introduction to delimiters and atoms in PHP regular expressions_PHP Tutorial
本节内容我们将介绍PHP中正则 表达式的基础语法:定界符和原子。内容包含了定界符的定义以及原子的定义和构成等等。其中原子的构成十分灵活,以便满足我们对处理字符串的需求。在这之 前,我们需要先了解一个正则表达式处理函数preg_match()来进行测试,以方便我们教程示例的进行。
先来看一下正则表达式的定界符、正则表达式的构成以及preg_match()函数:
1,正则表达式的定界符。
除了字母、数字和反斜线\以外的任何字符都可以为定界符号,比如 | |、//、{}、!!等等,但是需要注意,如果没有特殊需要,我们都使用正斜线//作为正则表达式的定界符号。
2,正则表达式的构成。
我们看一下这个公式:/原子和元字符/模式修正符
也就是说,正则表达式的原子和元字符都放在定界符之间,而模式修正符放在定界符之外。
3,preg_match()函数
我们会在后面进行详细解释,这里只是为了帮助测试,其返回一个布尔值,表示是否成功匹配。
了解完以上简单的内容,让我们进入正题。
正则表达式中的原子
什么是原子?原子是正则表达式的最基本组成单位,而且必须至少要包含一个原子。只要一个正则表达式可以单独使用的字符,就是原子。
这个概念可能看起来很模糊,没关系,下面我们来介绍一下正则表达式中原子的构成方式。
原子构成方式
1,所有打印(所有可以在屏幕上输出的字符串)和非打印字符(看不到的,比如空格,换行符等等)
2,如果所有有意义的字符,想做为原子使用,统统使用“\”转义字符进行转义即可。如:\. \* \+ \? \( \<\>。
注意:" \ "转义字符可以将有意义的字符转成没意义的字符,还可以将没意义的字符转为有意义的字符。如:\d表示任意一个十进制的数字。
3,在正则表达式中可以直接使用一些系统提供的代表范围的原子,如下面的表格所示:
代表范围的原子 | 说明 | 自定义原子表示法 |
\d | 表示任意一个十进制的数字 | [0-9] |
\D | 表示任意一个除数字这外的字符 | [^0-9] |
\s | 表示任意一个空白字符,空格、\n\r\t\f | [\n\r\t\f ] |
\S | 表示任意一个非空白 | [^\n\r\t\f ] |
\w | 表示任意一个字 a-zA-Z0-9_ | [a-zA-Z0-9_] |
\W | 表示任意一个非字,除了a-zA-Z0-9_以外的任意一个字符 | [^a-zA-Z0-9_] |
4. Customize the atom table (using square brackets []), which can match any atom in the square brackets.
In the above table, we have equivalently converted the range atoms provided by the system in a custom way. Since the system cannot provide all the atoms I need, it is necessary to customize the atom table. For example, if we want to match letters or numbers, we need to write the atoms as [a-zA-Z0-9].
Note here:
A, the symbol "-" represents the range, such as [a-z] represents the lowercase letters a to z, but never write it as [a- 9] This form!
B, the symbol "^" means negation, and must be placed at the beginning of the square brackets. For example, if we want to match non-digits, the atom is [^0-9].
Let’s take a look at an example of using regular expression atoms. The code is as follows:
$pattern = '/d/';//Number atomic table, which is the pattern of regular expressions
$string = 'dsadsadsa';//Requires matching String
if(preg_match($pattern, $string)){
echo "Regular expression{$pattern} and string{$string} Match successful";
}else{
echo "Regular expression {$pattern} and string {$string} failed to match< ;/span>";
}
?>
Note: If one atom in the custom atom table is matched by a string, the match is successful. Removing the square brackets of the custom atom table means matching the entire string. For example, '/abc/' means that the substring abc must be included in the string to be matched, and '/[abc]/' means that the string will be matched as long as it contains any one of the characters a, b, and c.
You can modify the pattern in the above example (that is, the pattern variable $pattern of the regular expression), and then verify the atoms of the regular expression we talk about in this section.
This section has finished introducing the delimiters and atoms of regular expressions. I believe that based on practice, you will already be able to use atoms of regular expressions. In the next section we will introduce metacharacters in PHP regular expressions, don’t miss it.

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,

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

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

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.
