跳轉至內容

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";
}
上一篇:正則表示式運算子 索引 下一篇:程式碼重用(模組)
華夏公益教科書