📜  PHP-正则表达式

📅  最后修改于: 2020-10-21 05:13:21             🧑  作者: Mango


正则表达式无非是字符本身的序列或模式。它们为模式匹配功能奠定了基础。

使用正则表达式,您可以搜索另一个字符串的特定字符串,可以将一个字符串替换为另一个字符串,并且可以将一个字符串拆分成许多块。

PHP提供了特定于两组正则表达式函数的函数,每组对应于某种类型的正则表达式。您可以根据自己的舒适度使用它们中的任何一种。

  • POSIX正则表达式
  • PERL样式正则表达式

POSIX正则表达式

POSIX正则表达式的结构与典型的算术表达式没有什么不同:各种元素(运算符)组合在一起形成更复杂的表达式。

最简单的正则表达式是一个与单个字符(例如g)匹配的字符串,例如g,haggle或bag。

让我们解释一下POSIX正则表达式中使用的几个概念。之后,我们将为您介绍与正则表达式相关的功能。

括号

在正则表达式的上下文中使用方括号([])具有特殊含义。它们用于查找一系列字符。

Sr.No Expression & Description
1

[0-9]

It matches any decimal digit from 0 through 9.

2

[a-z]

It matches any character from lower-case a through lowercase z.

3

[A-Z]

It matches any character from uppercase A through uppercase Z.

4

[a-Z]

It matches any character from lowercase a through uppercase Z.

上面显示的范围是一般范围;您还可以使用范围[0-3]匹配范围从0到3的任何十进制数字,或范围[bv]匹配范围从b到v的任何小写字符。

量词

方括号字符序列和单个字符的频率或位置可以用特殊字符表示。每个特殊字符都有特定的含义。 +,*,?,{int。 range}和$标志都遵循一个字符序列。

Sr.No Expression & Description
1

p+

It matches any string containing at least one p.

2

p*

It matches any string containing zero or more p’s.

3

p?

It matches any string containing zero or one p’s.

4

p{N}

It matches any string containing a sequence of N p’s

5

p{2,3}

It matches any string containing a sequence of two or three p’s.

6

p{2, }

It matches any string containing a sequence of at least two p’s.

7

p$

It matches any string with p at the end of it.

8

^p

It matches any string with p at the beginning of it.

例子

以下示例将清除您有关匹配字符的概念。

Sr.No Expression & Description
1

[^a-zA-Z]

It matches any string not containing any of the characters ranging from a through z and A through Z.

2

p.p

It matches any string containing p, followed by any character, in turn followed by another p.

3

^.{2}$

It matches any string containing exactly two characters.

4

(.*)

It matches any string enclosed within and .

5

p(hp)*

It matches any string containing a p followed by zero or more instances of the sequence php.

预定义字符范围

为了方便编程,可以使用几个预定义的字符范围,也称为字符类。字符类指定整个字符范围,例如,字母或整数集-

Sr.No Expression & Description
1

[[:alpha:]]

It matches any string containing alphabetic characters aA through zZ.

2

[[:digit:]]

It matches any string containing numerical digits 0 through 9.

3

[[:alnum:]]

It matches any string containing alphanumeric characters aA through zZ and 0 through 9.

4

[[:space:]]

It matches any string containing a space.

PHP的Regexp POSIX函数

PHP当前提供了七个使用POSIX样式正则表达式搜索字符串的函数-

Sr.No Function & Description
1 ereg()

The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise.

2 ereg_replace()

The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found.

3 eregi()

The eregi() function searches throughout a string specified by pattern for a string specified by string. The search is not case sensitive.

4 eregi_replace()

The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive.

5 split()

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

6 spliti()

The spliti() function operates exactly in the same manner as its sibling split(), except that it is not case sensitive.

7 sql_regcase()

The sql_regcase() function can be thought of as a utility function, converting each character in the input parameter string into a bracketed expression containing two characters.

PERL样式正则表达式

Perl样式的正则表达式与POSIX对应的正则表达式相似。 POSIX语法几乎可以与Perl样式的正则表达式函数互换使用。实际上,您可以使用上一POSIX部分介绍的任何量词。

让我们解释一下PERL正则表达式中使用的几个概念。之后,我们将向您介绍与正则表达式相关的功能。

元字符

元字符只是字母字符,后跟反斜杠,以使组合具有特殊含义。

例如,您可以使用’\ d’元字符/ / [[\ d] +)000 /搜索大笔款项,这里\ d将搜索任何数字字符字符串。

以下是可在PERL样式正则表达式中使用的元字符列表。

Character        Description
.              a single character
\s             a whitespace character (space, tab, newline)
\S             non-whitespace character
\d             a digit (0-9)
\D             a non-digit
\w             a word character (a-z, A-Z, 0-9, _)
\W             a non-word character
[aeiou]        matches a single character in the given set
[^aeiou]       matches a single character outside the given set
(foo|bar|baz)  matches any of the alternatives specified

修饰符

有几个修饰符可以使您使用正则表达式更容易,例如区分大小写,在多行中搜索等。

Modifier    Description
i     Makes the match case insensitive
m     Specifies that if the string has newline or carriage
    return characters, the ^ and $ operators will now
    match against a newline boundary, instead of a
    string boundary
o     Evaluates the expression only once
s     Allows use of . to match a newline character
x     Allows you to use white space in the expression for clarity
g     Globally finds all matches
cg     Allows a search to continue even after a global match fails

PHP的Regexp PERL兼容功能

PHP提供以下功能,用于使用与Perl兼容的正则表达式搜索字符串-

Sr.No Function & Description
1 preg_match()

The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.

2 preg_match_all()

The preg_match_all() function matches all occurrences of pattern in string.

3 preg_replace()

The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.

4 preg_split()

The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.

5 preg_grep()

The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.

6 preg_ quote()

Quote regular expression characters