Home Backend Development PHP Tutorial The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

Oct 18, 2021 pm 02:26 PM
php regular expression

In the previous article, I brought you "Learn to use PHP's List, each function and cooperation", which mainly explained how to use the list function and each function and what should be done between the two. When used together, I believe you have almost mastered it, so in this article we will take a look at regular expressions in PHP. I hope everyone has to help!

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

Regular expression is a logical formula, which is a "specified string" composed of some characters declared at the beginning and a combination of these characters. This The specified string is actually used to filter the string. It can be understood that when logging in as a user, we sometimes need to fill in specific data such as a verification code or phone number. At this time, we need to use regular expressions.

Although regular expressions look complicated, they are actually not difficult. Let’s take a look at them next.

The delimiters of regular expressions

#The first thing we have to learn is the delimiters of regular expressions. As the name suggests, The delimiter is a symbol that determines the boundary of a regular expression. Set a boundary, and the regular expression is within the boundary. At the same time, the delimiter of regular expressions is stipulated:

  • delimiter cannot be used, a-zA-Z0-9\ can be used, otherwise it can be used. And they must appear in pairs, with a beginning and an end.

The example is as follows:

$正则表达式$
%正则表达式%
/正则表达式/
Copy after login

What we need to pay attention to is that / is an escape character. When we need to match / in the regular expression, You can use \ to escape it. If you find it troublesome, you can directly use other delimiters such as:

$/$
Copy after login

The atoms of regular expressions

The atoms of regular expressions are regular expressions The smallest unit in the formula is what we need to match. In the integer expression we establish, there must be at least one atom.

In fact, it can be understood that all visible and invisible characters are atoms, such as spaces, carriage returns, line feeds, 0-9, punctuation marks, A-Za-z, and Chinese. These are atoms.

preg_match() function

Before talking about atoms in detail, we need to understand a function first, that ispreg_match

The preg_match() function in PHP can search and match strings based on defined regular expressions.

The syntax format is as follows:

preg_match ( string $正则 , string $字符串 [, array &$结果] )
Copy after login

According to $regex, which is the regular expression we defined, when matching $string, if it exists, the number of matches will be returned, and then the matching result will be placed in $In the results. If no result is found, 0 is returned.

Let’s take a look at it through an example:

<?php
//定义一个变量叫a,作为我们定义的正则表达式。
$a = &#39;/a/&#39;;
$b = &#39;abbcccddddeeeee&#39;;
if(preg_match($a, $b, $c)){
   echo &#39;匹配到了,结果为:&#39;;
   var_dump($c);
}else{
   echo &#39;没有匹配到&#39;;
}
?>
Copy after login

Output result:

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

As can be seen from the above example , we defined the variable a, hoping to match a, which happens to exist in $b, and the output through the if else statement was successful.

Another example:

<?php
//定义一个变量叫a,作为我们定义的正则表达式。
$a = &#39;/fff/&#39;;
$b = &#39;abbcccddddeeeee&#39;;
if(preg_match($a, $b, $c)){
   echo &#39;匹配到了,结果为:&#39;;
   var_dump($c);
}else{
   echo &#39;没有匹配到&#39;;
}
?>
Copy after login

Output result:

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

In the above example, we hope to match the string, but $ It does not exist in b, so there is no successful match. The if else statement outputs that the match is successful.

After knowing the basic usage of the preg_match() function, we can use it in combination with specially identified atoms.

Specially identified atoms

  • ##\d---matches a 0-9

  • \D---All characters except 0-9

  • ##\w

    ---a-zA- Z0-9_

  • ##\W-
  • --All characters except 0-9A-Za-z_

  • \s
  • ---Match all whitespace characters\n \t \r spaces

  • \S
  • ---Match all non-whitespace characters Characters

  • [ ]
  • ---Atoms in the specified range

    I will give you an example of their specific usage. Got it:
<?php
$a = &#39;/\d/&#39;;
$b = &#39;人生自古谁无4&#39;;
if(preg_match($a, $b, $c)){
   echo &#39;匹配到了,结果为:&#39;;
   var_dump($c);
}else{
   echo &#39;没有匹配到&#39;;
}
?>
Copy after login

Output result:


In the above example, the specially identified atom \d represents 0- If the number is 9, then there is a 4 in $b that needs to be matched, so the match is successful. The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

<?php
$a = &#39;/\w/&#39;;
$b = &#39;人生自古谁无死&#39;;
if(preg_match($a, $b, $c)){
   echo &#39;匹配到了,结果为:&#39;;
   var_dump($c);
}else{
   echo &#39;没有匹配到&#39;;
}
?>
Copy after login

Output result:


#In the above example, the specially identified atom \w represents a-zA-Z0-9_, There is no corresponding element in variable b, so the output result is not matched. The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

There is also a

[^]

character indicating characters that do not match the specified range.

The example is as follows:

<?php
$a = &#39;/[^0-9A-Za-z_]/&#39;;
$b = &#39;abbccc122333&#39;;
if(preg_match($a, $b, $c)){
   echo &#39;匹配到了,结果为:&#39;;
   var_dump($c);
}else{
   echo &#39;没有匹配到&#39;;
}
?>
Copy after login

Output result:

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

通过[^]字符匹配除0-9A-Za-z_以外的字符,未匹配到。

总结一下:

  • \w---[a-zA-Z0-9_]

  • \W---[^a-zA-Z0-9_]

  • \d---[0-9]

  • \D---[^0-9]

  • \s---[ \t\n\f\r]

  • \S---[^ \t\n\f\r]

正则表达式的元字符

在上面的示例中,我们能够看出通过匹配的话,只能匹配一个字符,但是在我们的日常使用中,通常会匹配多个字符,那这时候只通过我们的原子就不能达到我们的目的。就需要通过元字符来帮我们修饰原子,实现更多的功能。

  • *---代表匹配前面的一个原子,匹配0次或者任意多次前面的字符。

  • +---匹配一次或多次前面的一个字符

  • ?---前面的字符可有可无【可选】 有或没有

  • .---更标准一些应该把点算作原子。匹配除了\n以外的所有字符 或者。注:它的优先级最低了。

  • ^---必须要以抑扬符之后的字符串开始

  • $--- 必须要以$之前的字符结尾

  • \b---词边界

  • \B---非边界

  • {m}---有且只能出现m次

  • {n,m}---可以出现n到m次

  • {m,}---至少m次,最大次数不限制

  • ()---改变优先级或者将某个字符串视为一个整体,匹配到的数据取出来也可以使用它

接下来我们通过一些例子来实例看一下这些元字符的使用:

<?php
$a = &#39;/\d+/&#39;;
$b = "爱你10000年";
if(preg_match($a, $b, $c)){
   echo &#39;匹配到了,结果为:&#39;;
   var_dump($c);
}else{
   echo &#39;没有匹配到&#39;;
}
?>
Copy after login

输出结果:

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

通过元字符+的添加,匹配到了多次字符,\d+中d是匹配数字,+则表示最少匹配一次前面的字符。

正则表达式的模式修正符

通过原子和元字符的了解,我们已经完成了正则表达式的入门,但是这仍然不能代表正则表达式的真正实力,如果我们只希望正则表达式匹配一部分应该怎么办?有些特殊情况依然需要处理,这时候我们就要用到正则表达式的模式修正符。

下面列举一些常用的模式修正符:

  • i 模式中的字符将同时匹配大小写字母.

  • m 字符串视为多行

  • s 将字符串视为单行,换行符作为普通字符.

  • x 将模式中的空白忽略.

  • A 强制仅从目标字符串的开头开始匹配.

  • D 模式中的美元元字符仅匹配目标字符串的结尾.

  • U 匹配最近的字符串.

它的用法如下:

/正则表达式/模式修正符

接下来我们通过一些实例来看一下它的使用:

<?php  
    $a = &#39;/ABC/i&#39;; 
$b = &#39;8988abc12313&#39;;
$c = &#39;11111ABC2222&#39;; 
if(preg_match($a, $b, $d)){
     echo &#39;匹配到了,结果为:&#39;; 
    var_dump($d); }else{
     echo &#39;没有匹配到&#39;;
     }
 ?>
Copy after login

输出结果:

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

i可以让匹配的时候同时匹配大小写,那么接下来把匹配的$b换成$c试一下,我们看一下输出结果:

<?php  
    $a = &#39;/ABC/i&#39;; 
$b = &#39;8988abc12313&#39;;
$c = &#39;11111ABC2222&#39;; 
if(preg_match($a, $c, $d)){
     echo &#39;匹配到了,结果为:&#39;; 
    var_dump($d); }else{
     echo &#39;没有匹配到&#39;;
     }
 ?>
Copy after login

输出结果:

The exciting basics of regular expressions in PHP (detailed explanation with illustrations)

推荐学习:《PHP视频教程

The above is the detailed content of The exciting basics of regular expressions in PHP (detailed explanation with illustrations). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles