搜索
首页后端开发php教程PHP 正则表达式

PHP 正则表达式

Aug 29, 2024 pm 01:01 PM
php

正则表达式可以定义为在单行中生成的模式匹配算法。这些对于验证检查和模板识别是有影响的。元字符使用户能够处理复杂的模式。因此,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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP的当前状态:查看网络开发趋势PHP的当前状态:查看网络开发趋势Apr 13, 2025 am 12:20 AM

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP与其他语言:比较PHP与其他语言:比较Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能PHP与Python:核心功能Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP:网络开发的关键语言PHP:网络开发的关键语言Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP:许多网站的基础PHP:许多网站的基础Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

超越炒作:评估当今PHP的角色超越炒作:评估当今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

PHP中的弱参考是什么?什么时候有用?PHP中的弱参考是什么?什么时候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

解释PHP中的__ Invoke Magic方法。解释PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。