Metacharacters

^ beginning of string
$ end of string
. any character except newline
* match 0 or more times
+

match 1 or more times

? match 0 or 1 times; or shortest match
| alternative
() grouping; "storing"
[] set of characters
{}

repetition modifier

\ quote or special

To present a metacharacter as a data character standing for itself, precede it with \ (e.g. \. matches the full stop character . only).


Repetition

a* zero or more a’s
a+ one or more a’s
a? zero or one a’s (i.e., optional a)
a{m} exactly m a’s
a{m,} at least m a’s
a{m,m} at least m but at most na’s
repetition? same as repetition but theshortest match is taken

Read the notation a’s as “occurrences of strings, each of which matches the pattern a”. Read repetition as any of the repetition expressions listed above it. Shortest match means that the shortest string matching the pattern is taken. The default is “greedy matching”, which finds the longest match. Therepetition? construct was introduced in Perl version 5.


Calling these "wildcards" may actually conflict with the theoretical grammar and syntax of Perl, but in fact is the most intuitive way to think of it, and will not lead to any coding mistakes.

.   Match any character
\w  Match "word" character (alphanumeric plus "_")
\W  Match non-word character
\s  Match whitespace character
\S  Match non-whitespace character
\d  Match digit character
\D  Match non-digit character
\t  Match tab
\n  Match newline
\r  Match return
\f  Match formfeed
\a  Match alarm (bell, beep, etc)
\e  Match escape
\021  Match octal char ( in this case 21 octal)
\xf0  Match hex char ( in this case f0 hexidecimal)

You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power:

*      Match 0 or more times
+      Match 1 or more times
?      Match 1 or 0 times
{n}    Match exactly n times
{n,}   Match at least n times
{n,m}  Match at least n but not more than m times


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Code                Meaning
----------          --------------------------------------------
\w                  Alphanumeric Characters
\W                  Non-Alphanumeric Characters
\s                  White Space
\S                  Non-White Space
\d                  Digits
\D                  Non-Digits 
\b                  Word Boundary
\B                  Non-Word Boundary
\A or ^             At the beginning of a string
\Z or $             At the end of a string
.                   Match Any single character
*                   Zero or more occurrences
?                   Zero or one Occurences
+                   one or more occurences
{N}                 Exactly N occurences
{N,M}               Between N and M occurences
.*<thingy>          Greedy Match, up to the last thingy
.*?<thingy>         Non-Greedy match, up to the first thingy
[set_of_things]     Match any item in the set
[^set_of_things]    Does not match anything in the set
(some_expression)   Tag an expression
$1..$N              Tagged expressions used in substitutions