PHP 正则表达式

PHPz
发布: 2024-08-29 13:01:52
原创
392 人浏览过

正则表达式可以定义为在单行中生成的模式匹配算法。这些对于验证检查和模板识别是有影响的。元字符使用户能够处理复杂的模式。因此,PHP 对正则表达式的支持有助于提高 PHP 编程的代码质量。任何正则表达式都是通用模式或一组字符的序列,用于针对给定的主题字符串提供模式匹配功能。它也称为 RegExp 或 RegEx。它也被认为是基于模式表示法的小型编程语言,用于文本字符串解析。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

PHP函数中的2组正则表达式

下面给出支持2组正则表达式:

  • POSIX 正则表达式
  • PERL 风格正则表达式

1. POSIX 正则表达式

这被定义为字符集,其中任何单个字符都需要与输入字符串匹配。这些表达式在 [].

中定义

示例:

  • [0-9]: 这旨在过滤从 0 到 9 的任何十进制字符串。
  • [a-Z]: 这旨在过滤从小写“a”到大写“Z”的任何字符。

为了使过滤器更加具体,开发了一种标准语法,包括正则表达式和特殊字符,称为量词。它还提供有关频率的信息,即括号字符或一组字符的出现或实例数量以及数量。

查找不同量词描述的表格:

Quantifier Description
S+ Filters out a string having at least one ‘s’.
S* Filters out a string having zero or more ‘s’.
S? Filters out a string having zero or one ‘s’.
S{N} Filters out a string having a sequence of N ‘s’.
S$ Filters out a string having ‘s’ at the end.
^S Filters out a string having ‘s’ at the beginning.
量词

描述

S+ 过滤掉至少有一个“s”的字符串。 S* 过滤掉具有零个或多个“s”的字符串。 S? 过滤掉包含零个或一个“s”的字符串。 S{N} 过滤出具有 N 个“s”序列的字符串。 S$ 过滤掉末尾带有“s”的字符串。 ^S 过滤掉以“s”开头的字符串。 表> PHP 还支持预定义字符范围/类的匹配功能。
predefined character class Description
[[:space:]] Filters out a string having a space.
[[:alpha:]] Filters out a string having alphabetic characters a-A through z-Z.
[[:digit:]] Filters out a string having numerical 0 to 9.
[[:alnum:]] Filters out a string having alphanumeric characters a-A through z-Z and numerical 0 to 9.
示例: 预定义字符类 描述 [[:空格:]] 过滤掉含有空格的字符串。 [[:alpha:]] 过滤掉包含字母字符 a-A 到 z-Z 的字符串。 [[:数字:]] 过滤掉数字0到9的字符串。 [[:alnum:]] 过滤掉包含字母数字字符 a-A 到 z-Z 以及数字 0 到 9 的字符串。 表>

对于 POSIX 正则表达式,PHP 集成了各种函数,可以使用 POSIX 风格的正则表达式执行各种操作。

功能说明如下表:

POSIX Regex function Description
ereg() Used to search a string specified by or by pattern and to return true if the matching is found.
ereg_replace() Used to search a string specified by or by pattern and replace with replacement if the matching is found.
eregi() Used to perform non-case sensitive search for a string specified by or by pattern and to return true if the matching is found.
eregi_replace() Used to perform non-case sensitive for a string specified by or by pattern and replace with replacement if the matching is found.
split() Used to divide the string into separate elements based on boundaries that matches the searching pattern.
spliti() Used to perform non-case sensitive for the string to divide it into separate elements based on boundaries that matches the searching pattern.
sql_regcase() A utility function that convert each character from the input value into a bracketed expression making two characters.

2. PERL 风格正则表达式

这种类型的正则表达式模式类似于 POSIX 正则表达式,但使用元字符和标识符创建。此正则表达式的语法可与 POSIX 样式互换。

a。元字符:前面带有表示特定含义的反斜杠的字母字符称为元字符。

PHP 脚本支持多种元字符,用作 Perl 类型正则表达式,如下所述:

Meta character Description
. Single character
d A digit character
D Non-digit character
s white space character e.g. SPACE, NEW LINE, TAB
S Non- white space character
w A word character
W Non-word character
[aeiou] Filters the matched character out of the given set
[^aeiou] Filters the unmatched character out of the given set
(set1|set2|set3) Filters the matched element that matches to any of the given alternatives
元字符

描述 。 单个字符 d 数字字符 D 非数字字符 s 空白字符,例如空格键、换行符、制表符 S 非空白字符 w 一个单词字符 西 非单词字符 [aeiou] 从给定集合中过滤出匹配的字符 [^aeiou] 从给定集合中过滤掉不匹配的字符 (设置1|设置2|设置3) 过滤与任何给定替代项匹配的匹配元素 表>

b。修饰符:

这些元素使用户能够利用正则表达式获得额外的灵活性。
Modifier Description
g Finds matchings globally.
cg Enable continue global search even after matching fails.
i Instructs to perform case insensitive search.
s Use the character ‘.’ to search for new line character.
m In case of input string containing new line or carriage return character, ‘^’ and ‘$’ are used to match for new line boundary.
x Permits to use white space to improve the clarity of the expression.
o Restrict the evaluation of the expression to occur only once.
下表列出了各种修饰符及其功能: 修饰符 描述 克 在全球范围内查找匹配项。 cg 即使匹配失败也能继续进行全局搜索。 我 指示执行不区分大小写的搜索。 s 使用字符“.”搜索换行符。 米 如果输入字符串包含换行符或回车符,则使用“^”和“$”来匹配换行边界。 x 允许使用空白来提高表达式的清晰度。 o 限制表达式的计算仅发生一次。 表>

与 POSIX 正则表达式函数类似,PHP 也提供了一些与 PERL 风格正则表达式兼容的特定函数。

一些主要功能讨论如下:

PERL style regexpcompitable function Description
preg_match() Return the first occurrence of the matching pattern.
preg_match_all() Return all occurrences of the matching pattern.
preg_split() Splits the string input into several elements based on the given regexp pattern as input.
Preg_quote() Used to quote the characters of the regex.
preg_grep() Used to find all the matching elements from array input.
preg_replace() Used to find matching element and replace it with the given replacement.

PHP 正则表达式示例

下面的示例演示了应用程序

代码片段旨在扫描输入字符串,并通过将给定的正则表达式定义为边界,将给定的输入拆分为多个元素。

代码:

<?php
// Declaring a regex
$regex = "([0-9]+)";
// Defining the input string
$inputstr = "String_a 1 String_b 2 String_c 3";
//Splitting the input string based on matching regex expression
$result = preg_split ($regex, $inputstr);
// Displaying result
echo $result[0];
echo "\n";
echo $result[1];
echo "\n";
echo $result[2];
echo "\n";
?>
登录后复制

输出

函数 preg_split() 将输入字符串分成 3 部分,元素“1”、“2”和“3”被标记为边界。

PHP 正则表达式

以上是PHP 正则表达式的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!