跳转至内容

Perl 编程/正则表达式参考

来自维基教科书,开放的书籍,开放的世界
上一篇:正则表达式运算符 索引 下一篇:代码重用(模块)

带 Perl 示例的正则表达式

[编辑 | 编辑源代码]
元字符 描述 示例
请注意,所有 if 语句都返回一个 TRUE 值
. 匹配任意字符,但不是换行符。
$string1 = "Hello World\n";
if ($string1 =~ m/...../) {
print "$string1 has length >= 5\n";
}
( ) 将一系列模式元素分组到一个元素中。当你在括号内匹配一个模式时,你可以使用任何$1, $2, $9以后参考之前匹配的模式。

程序

$string1 = "Hello World\n";
if ($string1 =~ m/(H..).(o..)/) {
print "We matched '$1' and '$2'\n";
}

输出

We matched 'Hel' and 'o W';
+ 匹配前面的模式元素一次或多次。
$string1 = "Hello World\n";
if ($string1 =~ m/l+/) {
print "There are one or more consecutive l's in $string1\n";
}
? 匹配零次或一次。
$string1 = "Hello World\n";
if ($string1 =~ m/H.?e/) {
print "There is an 'H' and a 'e' separated by ";
print "0-1 characters (Ex: He Hoe)\n";
}
? 匹配*, +,或{M,N}尽可能少的次数匹配之前的正则表达式。
$string1 = "Hello World\n";
if ($string1 =~ m/(l+?o)/) {
print "The non-greedy match with one or more 'l'
print "followed by an 'o' is 'lo', not 'llo'.\n";
}
* 匹配零次或多次。
$string1 = "Hello World\n";
if ($string1 =~ 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。
$string1 = "Hello World\n";
if ($string1 =~ m/l{1,2}/) {
print "There exists a substring with at least one";
print "and at most two l's in $string1\n";
}
[...] 表示一组可能的匹配。
$string1 = "Hello World\n";
if ($string1 =~ m/[aeiou]+/) {
print "$string1 contains a one or more";
print "vowels\n";
}
[^...] 匹配方括号中没有的任何字符。
$string = "Sky.";
if (String =~ /[^aeiou]/) {
print "$string doesn't contain any vowels";
}
| 匹配左操作数或右操作数中的一个。
$string1 = "Hello World\n";
if ($string1 =~ m/(Hello|Hi)/) {
print "Hello or Hi is ";
print "contained in $string1";
}
\b 匹配单词边界。
$string1 = "Hello World\n";
if ($string1 =~ m/ello?\b/) {
print "There is a word that ends with";
print " 'ello'\n";
} else {
print "There are no words that end with";
print "'ello'\n";
}
\w 匹配字母数字,包括"_".
$string1 = "Hello World\n";
if ($string1 =~ m/\w/) {
print "There is at least one alpha-";
print "numeric char in $string1 (A-Z, a-z, 0-9, _)\n";
}
\W 匹配非字母数字字符。
$string1 = "Hello World\n";
if ($string1 =~ m/\W/) {
print "The space between Hello and ";
print "World is not alphanumeric\n";
}
\s 匹配空白字符(空格、制表符、换行符、换页符)
$string1 = "Hello World\n";
if ($string1 =~ m/\s.*\s/) {
print "There are TWO whitespace ";
print "characters separated by other characters in $string1";
}
\S 匹配除空白字符以外的任何字符。
$string1 = "Hello World\n";
if ($string1 =~ m/\S.*\S/) {
print "There are TWO non-whitespace ";
print "characters separated by other characters in $string1";
}
\d 匹配数字,与[0-9].
$string1 = "99 bottles of beer on the wall.";
if ($string1 =~ m/(\d+)/) {
print "$1 is the first number in '$string1'\n";
}
'''Output:'''
99 is the first number in '<tt>99 bottles of beer on the wall.</tt>'
\D 匹配非数字。
$string1 = "Hello World\n";
if ($string1 =~ m/\D/) {
print "There is at least one character in $string1";
print "that is not a digit.\n";
}
^ 匹配行或字符串的开头。
$string1 = "Hello World\n";
if ($string1 =~ m/^He/) {
print "$string1 starts with the characters 'He'\n";
}
$ 匹配行或字符串的结尾。
$string1 = "Hello World\n";
if ($string1 =~ m/rld$/) {
print "$string1 is a line or string";
print "that ends with 'rld'\n";
}
上一篇:正则表达式运算符 索引 下一篇:代码重用(模块)
华夏公益教科书