正则表达式/Perl 兼容正则表达式
外观
< 正则表达式
Perl 比甚至 POSIX 扩展正则表达式 语法拥有更丰富且更可预测的语法。其可预测性体现在 \ 始终用于转义非字母数字字符。Perl 可以指定但 POSIX 不能指定的一个示例是匹配的一部分是否贪婪。例如,在模式 /a.*b/ 中,.* 将匹配尽可能多的字符,而在模式 /a.*?b/ 中,.*? 将匹配尽可能少的字符。因此,对于字符串 "a bad dab",第一个模式将匹配整个字符串,而第二个模式只匹配 "a b"。
由于这些原因,许多其他实用程序和应用程序都采用了与 Perl 非常类似的语法。例如,Java、Ruby、Python、PHP、exim、BBEdit,甚至微软的 .NET Framework 都使用与 perl 中类似的正则表达式语法。并非所有“Perl 兼容”的正则表达式实现都相同,并且许多实现只包含 Perl 特性的子集。
约定 在示例中使用:字符 'm' 并不总是需要指定 perl 匹配操作。例如,m/[^abc]/ 也可以表示为 /[^abc]/。只有当用户希望在不使用正斜杠作为正则表达式分隔符的情况下指定匹配操作时,'m' 才必不可少。有时,为了避免“分隔符冲突”,指定替代的正则表达式分隔符很有用。有关更多详细信息,请参阅 'perldoc perlre'。
metacharacter(s) ;; the metacharacters column specifies the regex syntax being demonstrated =~ m// ;; indicates a regex match operation in perl =~ s/// ;; indicates a regex substitution operation in perl
在下表标题中,“M-c” 代表“元字符”。
| M-c | 描述 | 例子 所有 if 语句都返回 TRUE 值。 |
|---|---|---|
| . | 通常匹配除换行符之外的任何字符。在方括号内,点是字面字符。 |
if ("Hello World\n" =~ m/...../) {
print "Yep"; # Has length >= 5\n";
}
|
| ( ) | 将一系列模式元素分组为一个元素。当您匹配括号内的模式时,您可以使用 $1、$2 等在稍后引用之前匹配的模式。 |
if ("Hello World\n" =~ m/(H..).(o..)/) {
print "We matched '$1' and '$2'\n";
}
输出
|
| + | 匹配前面的模式元素一次或多次。 |
if ("Hello World\n" =~ m/l+/) {
print "One or more \"l\"'s in the string\n";
}
|
| ? | 匹配前面的模式元素零次或一次。 |
if ("Hello World\n" =~ m/H.?e/) {
print "There is an 'H' and a 'e' separated by ";
print "0-1 characters (Ex: He Hoe)\n";
}
|
| ? | 修改前面的 *、+ 或 {M,N} 的正则表达式,使其匹配尽可能少的次数。 |
if ("Hello World\n" =~ m/(l.+?o)/) {
print "Yep"; # The non-greedy match with 'l' followed
# by one or more characters is 'llo' rather than 'llo wo'.
}
|
| * | 匹配前面的模式元素零次或多次。 |
if ("Hello World\n" =~ m/el*o/) {
print "There is an 'e' followed by zero to many ";
print "'l' followed by 'o' (eo, elo, ello, elllo)\n";
}
|
| {M,N} | 表示最小匹配次数 M 和最大匹配次数 N。 |
if ("Hello World\n" =~ m/l{1,2}/) {
print "There is a substring with at least 1 ";
print "and at most 2 l's in the string\n";
}
|
| [...] | 表示一组可能的字符匹配。 |
if ("Hello World\n" =~ m/[aeiou]+/) {
print "Yep"; # Contains one or more vowels
}
|
| | | 分隔备选匹配项。 |
if ("Hello World\n" =~ m/(Hello|Hi|Pogo)/) {
print "At least one of Hello, Hi, or Pogo is ";
print "contained in the string.\n";
}
|
| \b | 匹配单词边界。 |
if ("Hello World\n" =~ m/llo\b/) {
print "There is a word that ends with 'llo'\n";
}
|
| \w | 匹配字母数字字符,包括“_”。 |
if ("Hello World\n" =~ m/\w/) {
print "There is at least one alphanumeric ";
print "character in the string (A-Z, a-z, 0-9, _)\n";
}
|
| \W | 匹配非字母数字字符,不包括“_”。 |
if ("Hello World\n" =~ m/\W/) {
print "The space between Hello and ";
print "World is not alphanumeric\n";
}
|
| \s | 匹配空白字符(空格、制表符、换行符、换页符) |
if ("Hello World\n" =~ m/\s.*\s/) {
print "There are TWO whitespace characters, which may";
print " be separated by other characters, in the string.";
}
|
| \S | 匹配除空白字符之外的任何字符。 |
if ("Hello World\n" =~ m/\S.*\S/) {
print "Contains two non-whitespace characters " .
"separated by zero or more characters.";
}
|
| \d | 匹配数字,与 [0-9] 相同。 |
if ("99 bottles of beer on the wall." =~ m/(\d+)/) {
print "$1 is the first number in the string'\n";
}
|
| \D | 匹配非数字。 |
if ("Hello World\n" =~ m/\D/) {
print "There is at least one character in the string";
print " that is not a digit.\n";
}
|
| ^ | 匹配行或字符串的开头。 |
if ("Hello World\n" =~ m/^He/) {
print "Starts with the characters 'He'\n";
}
|
| $ | 匹配行或字符串的结尾。 |
if ("Hello World\n" =~ m/rld$/) {
print "Is a line or string ";
print "that ends with 'rld'\n";
}
|
| \A | 匹配字符串的开头(但不是内部行)。 |
if ("Hello\nWorld\n" =~ m/\AH/) {
print "Yep"; # The string starts with 'H'.
}
|
| \Z | 匹配字符串的结尾(但不是内部行)。 |
if ("Hello\nWorld\n"; =~ m/d\n\Z/) {
print "Yep"; # Ends with 'd\\n'\n";
}
|
| [^...] | 匹配除方括号内字符之外的每个字符。 |
if ("Hello World\n" =~ m/[^abc]/) {
print "Yep"; # Contains a character other than a, b, and c.
}
|
使用 Perl 正则表达式语法的工具和语言包括
- Perl 正则表达式 在 perl.org
- Perl 兼容正则表达式库 在 pcre.org
- Perl 正则表达式语法 在 boost.org
- W:正则表达式#Perl