Php 简明教程
PHP - Regular Expressions
正则表达式本身不过是字符的序列或模式。它们为模式匹配功能提供基础。
Regular expressions are nothing more than a sequence or pattern of characters itself. They provide the foundation for pattern-matching functionality.
使用正则表达式,可以在另一个字符串中搜索特定字符串,可以用另一个字符串替换一个字符串,还可以将一个字符串拆分成许多块。
Using regular expression you can search a particular string inside a another string, you can replace one string by another string and you can split a string into many chunks.
PHP 提供特定于两组正则表达式函数的函数,每个函数都对应一定类型的正则表达式。您可以根据您的喜好使用其中任何一个。
PHP offers functions specific to two sets of regular expression functions, each corresponding to a certain type of regular expression. You can use any of them based on your comfort.
-
POSIX Regular Expressions
-
PERL Style Regular Expressions
POSIX Regular Expressions
POSIX 正则表达式的结构与典型算术表达式的结构并无不同:将各种元素(运算符)组合起来以形成更复杂的表达式。
The structure of a POSIX regular expression is not dissimilar to that of a typical arithmetic expression: various elements (operators) are combined to form more complex expressions.
最简单的正则表达式是匹配单个字符的正则表达式,如 g,在诸如 g、haggle 或 bag 等字符串中。
The simplest regular expression is one that matches a single character, such as g, inside strings such as g, haggle, or bag.
让我们对 POSIX 正则表达式中使用的几个概念进行解释。之后,我们将向您介绍正则表达式相关函数。
Lets give explanation for few concepts being used in POSIX regular expression. After that we will introduce you with regular expression related functions.
Brackets
括号 ([]) 在正则表达式中使用时具有特殊的含义。它们用于查找字符范围。
Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.
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 的任何十进制数字,或使用范围 [b-v] 以匹配从 b 到 v 的任何小写字母。
The ranges shown above are general; you could also use the range [0-3] to match any decimal digit ranging from 0 through 3, or the range [b-v] to match any lowercase character ranging from b through v.
Quantifiers
用方括号括起来的字符序列和单个字符出现的频率或位置可以用特殊字符表示。每个特殊字符都有一个特定含义。+、*、?、{int. range} 和 $ 标志都出现在字符序列之后。
The frequency or position of bracketed character sequences and single characters can be denoted by a special character. Each special character having a specific connotation. The +, *, ?, {int. range}, and $ flags all follow a character sequence.
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. |
Examples
以下示例将阐明您对匹配字符的概念。
Following examples will clear your concepts about matching characters.
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 |
<b>(.)</b>* It matches any string enclosed within <b> and </b>. |
5 |
p(hp)* It matches any string containing a p followed by zero or more instances of the sequence php. |
Predefined Character Ranges
为了方便你的编程,提供了几个预定义的字符范围,也称为字符类。字符类指定了一整个字符范围,例如,字母表或整数集 -
For your programming convenience several predefined character ranges, also known as character classes, are available. Character classes specify an entire range of characters, for example, the alphabet or an integer set −
PHP’s Regexp POSIX Functions
PHP 目前提供七个函数用于使用 POSIX 风格的正则表达式搜索字符串 -
PHP currently offers seven functions for searching strings using POSIX-style regular expressions −
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 Style Regular Expressions
Perl 风格的正则表达式类似于 POSIX 对应表达式。POSIX 语法几乎可以与 Perl 风格的正则表达式函数互换使用。事实上,你可以使用先前 POSIX 部分中介绍的任何限定符。
Perl-style regular expressions are similar to their POSIX counterparts. The POSIX syntax can be used almost interchangeably with the Perl-style regular expression functions. In fact, you can use any of the quantifiers introduced in the previous POSIX section.
让我们对 PERL 正则表达式中使用的一些概念进行说明。之后,我们会向你介绍与正则表达式相关的函数。
Lets give explanation for few concepts being used in PERL regular expressions. After that we will introduce you wih regular expression related functions.
Meta characters
元字符只是一个字母字符,前置反斜杠,作用是给组合赋予一个特殊含义。
A meta character is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning.
例如,你可以使用“\d”元字符搜索大额资金: /([\d]+)000/ ,此处 \d 将搜索任何数字字符的字符串。
For instance, you can search for large money sums using the '\d' meta character: /([\d]+)000/, Here \d will search for any string of numerical character.
以下是可以用于 PERL 风格正则表达式中的元字符列表。
Following is the list of meta characters which can be used in PERL Style Regular Expressions.
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
Modifiers
有几个修饰符可用,它们可以使你和正则表达式的合作更容易,比如区分大小写、在多行中搜索等。
Several modifiers are available that can make your work with regexps much easier, like case sensitivity, searching in multiple lines etc.
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’s Regexp PERL Compatible Functions
PHP 提供以下函数,用于使用 Perl 兼容正则表达式搜索字符串 −
PHP offers following functions for searching strings using Perl-compatible regular expressions −
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 |