Content deleted Content added
m Reverted edits by 41.153.203.177 (talk) to last version by Yobot |
|||
Line 34:
<td>Normally matches any character except a newline. Within square brackets the dot is literal.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/...../) {
print "$string1 has length >= 5\n";
}
</
</tr>
Line 46:
<td>Groups a series of pattern elements to a single element. When you match a pattern within parentheses, you can use any of $1, $2, ... later to refer to the previously matched pattern.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/(H..).(o..)/) {
print "We matched '$1' and '$2'\n";
}
</
We matched 'Hel' and 'o W';
</
</tr>
Line 60:
<td>Matches the preceding pattern element one or more times.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/l+/) {
print "There are one or more consecutive letter \"l\"'s in $string1\n";
}
</
There are one or more consecutive letter "l"'s in Hello World
</
</tr>
Line 75:
<td>Matches the preceding pattern element zero or one times.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/H.?e/) {
Line 81:
print "0-1 characters (Ex: He Hoe)\n";
}
</
</tr>
Line 89:
to match as few times as possible.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/(l.+?o)/) {
Line 95:
print "more characters is 'llo' rather than 'llo wo'.\n";
}
</
</tr>
Line 102:
<td>Matches the preceding pattern element zero or more times.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/el*o/) {
Line 108:
print "'l' followed by 'o' (eo, elo, ello, elllo)\n";
}
</
</tr>
Line 115:
<td>Denotes the minimum M and the maximum N match count.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/l{1,2}/) {
Line 121:
print "and at most 2 l's in $string1\n";
}
</
</td>
</tr>
Line 129:
<TD>Denotes a set of possible character matches.</TD>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/[aeiou]+/) {
print "$string1 contains one or more vowels.\n";
}
</
</tr>
Line 141:
<td>Separates alternate possibilities.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/(Hello|Hi|Pogo)/) {
Line 147:
print "contained in $string1.\n";
}
</
</tr>
Line 154:
<td>Matches a word boundary.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/llo\b/) {
print "There is a word that ends with 'llo'\n";
}
</
</tr>
Line 166:
<td>Matches an alphanumeric character, including "_"; same as [A-Za-z0-9_]</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/\w/) {
Line 172:
print "character in $string1 (A-Z, a-z, 0-9, _)\n";
}
</
</tr>
Line 179:
<td>Matches a '''non'''-alphanumeric character, excluding "_"; same as [^A-Za-z0-9_]</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/\W/) {
Line 185:
print "World is not alphanumeric\n";
}
</
</tr>
Line 192:
<td>Matches a whitespace character (space, tab, newline, form feed)</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/\s.*\s/) {
Line 198:
print " be separated by other characters, in $string1";
}
</
</tr>
Line 205:
<td>Matches anything BUT a whitespace.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/\S.*\S/) {
Line 211:
print " may be separated by other characters, in $string1";
}
</
</tr>
Line 218:
<td>Matches a digit; same as [0-9].</td>
<td align="left">
<source lang="perl">
$string1 = "99 bottles of beer on the wall.";
if ($string1 =~ m/(\d+)/) {
print "$1 is the first number in '$string1'\n";
}
</
99 is the first number in '99 bottles of beer on the wall.'
</
</tr>
Line 232:
<td>Matches a non-digit; same as [^0-9].</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/\D/) {
Line 238:
print " that is not a digit.\n";
}
</
</tr>
Line 245:
<td>Matches the beginning of a line or string.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/^He/) {
print "$string1 starts with the characters 'He'\n";
}
</
</tr>
Line 257:
<td>Matches the end of a line or string.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/rld$/) {
Line 263:
print "that ends with 'rld'\n";
}
</
</tr>
Line 270:
<td>Matches the beginning of a string (but not an internal line).</td>
<td align="left">
<source lang="perl">
$string1 = "Hello\nWorld\n";
if ($string1 =~ m/\AH/) {
Line 276:
print "that starts with 'H'\n";
}
</
</tr>
Line 283:
<td>Matches the end of a string (but not an internal line).<br/> see Perl Best Practices — Page 240</td>
<td align="left">
<source lang="perl">
$string1 = "Hello\nWorld\n";
if ($string1 =~ m/d\n\z/) {
Line 289:
print "that ends with 'd\\n'\n";
}
</
</tr>
Line 296:
<td>Matches every character except the ones inside brackets.</td>
<td align="left">
<source lang="perl">
$string1 = "Hello World\n";
if ($string1 =~ m/[^abc]/) {
Line 302:
print "a, b, and c\n";
}
</
</tr>
|