


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.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor