正则表达式可以定义为在单行中生成的模式匹配算法。这些对于验证检查和模板识别是有影响的。元字符使用户能够处理复杂的模式。因此,PHP 对正则表达式的支持有助于提高 PHP 编程的代码质量。任何正则表达式都是通用模式或一组字符的序列,用于针对给定的主题字符串提供模式匹配功能。它也称为 RegExp 或 RegEx。它也被认为是基于模式表示法的小型编程语言,用于文本字符串解析。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
下面给出支持2组正则表达式:
这被定义为字符集,其中任何单个字符都需要与输入字符串匹配。这些表达式在 [].
中定义示例:
为了使过滤器更加具体,开发了一种标准语法,包括正则表达式和特殊字符,称为量词。它还提供有关频率的信息,即括号字符或一组字符的出现或实例数量以及数量。
查找不同量词描述的表格:
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. |
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. |
对于 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. |
这种类型的正则表达式模式类似于 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 |
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. |
与 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 // 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中文网其他相关文章!