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}'d 正則表示式,出現次數儘可能少。 |
$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";
}
|