21xrx.com
2024-09-20 00:47:36 Friday
登录
文章检索 我的文章 写文章
PHP 正则表达式
2021-07-22 08:58:30 深夜i     --     --
P H P


什么是正则表达式?

正则表达式是形成搜索模式的字符序列。 当您在文本中搜索数据时,您可以使用此搜索模式来描述您想找什么。

正则表达式可以是单个字符,也可以是更复杂的模式。

正则表达式可用于执行所有类型的文本搜索和文本替换 操作。


句法

在 PHP 中,正则表达式是由分隔符、模式和可选组成的字符串 修饰符。

$exp = "/21xrx/i";

在上面的例子中, / 是分隔符, 21xrx 是正在搜索的模式, i 是一个修饰符,使搜索不区分大小写。

分隔符可以字母、数字、反斜杠或空格的任何字符。 这 最常见的分隔符是正斜杠 (/),但是当您的模式包含向前 斜线 可以方便地选择其他分隔符,例如 # 或 ~。


正则表达式函数

PHP 提供了多种允许您使用正则表达式的函数。 这 preg_match()、preg_match_all() 和 preg_replace() 函数是一些 最常用的:

Function Description
preg_match() Returns 1 if the pattern was found in the string and 0 if not
preg_match_all() Returns the number of times the pattern was found in the string, which may also be 0
preg_replace() Returns a new string where matched patterns have been replaced with another string

使用 preg_match()

preg_match() 函数将告诉您字符串是否包含模式的匹配项。

使用正则表达式对字符串中的“w3schools”进行不区分大小写的搜索:

<?php
$str = "Visit 21xrx";
$pattern = "/21xrx/i";
echo preg_match($pattern, $str); // Outputs 1
?>

使用 preg_match_all()

preg_match_all() 函数会告诉你在一个模式中找到了多少匹配字符串。

使用正则表达式进行不区分大小写的计数 “ain”在字符串中出现的次数:

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>

使用 preg_replace()

preg_replace() 函数将替换字符串中模式的所有匹配项 另一个字符串。

使用不区分大小写的正则表达式将 Microsoft 替换为 字符串中的 21xrx:

<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, "21xrx", $str); // Outputs "Visit 21xrx!"
?>


正则表达式修饰符

修饰符可以改变搜索的执行方式。

Modifier Description
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line)
u Enables correct matching of UTF-8 encoded patterns

正则表达式模式

括号用于查找一系列字符:

Expression Description
[abc] Find one character from the options between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find one character from the range 0 to 9

元字符

元字符是具有特殊含义的字符:

Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx

量词

量词定义数量:

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{x} Matches any string that contains a sequence of X n's
n{x,y} Matches any string that contains a sequence of X to Y n's
n{x,} Matches any string that contains a sequence of at least X n's

注意:如果您的表达式需要搜索特殊字符之一,您可以使用 反斜杠 ( \ ) 来逃避它们。 例如,要搜索一个或多个问号,您可以使用以下命令 表达式:$pattern = '/\?+/';


分组

您可以使用括号 ( ) 将量词应用于整个模式。 它们也可以使用 选择要用作匹配的模式部分。

通过查找 ba 后跟,使用分组来搜索单词“banana” na 的两个实例:

<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str); // Outputs 1
?>

完整的正则表达式参考

如需完整参考,请访问我们的完整 PHP 正则表达式参考。

该参考包含所有正则表达式函数的说明和示例。


 

 

  
  
下一篇: PHP 运算符

评论区

{{item['qq_nickname']}}
()
回复
回复