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
ma’s

a{m,}

at least
ma’s

a{m,n}

at least
m but at most n a’s

repetition?

same as repetition
but the shortest 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.
The repetition? construct was
introduced in Perl version 5.

an a followed by an optional
b
followed by a c; that is, either abc or
ac

a.c

an a followed by any single character (not newline)
followed by a c

a\.c

a.c
exactly

[abc]

any one of a, b and c

[Aa]bc

either of Abc and abc

[abc]+

any (nonempty) string of a’s, b’s
and c’s (such as a, abba,
acbabcacaa)

[^abc]+

any (nonempty) string which does not
contain any of a, b
and c (such as
defg)

\d\d

any two decimal digits, such as 42;
same as \d{2}

\w+

a “word”: a nonempty sequence of alphanumeric characters and
low lines (underscores), such as foo and
12bar8 and foo_1

100\s*mk

the strings 100 and mk
optionally separated by any amount of white space
(spaces, tabs, newlines)

abc\b

abc when followed by a word
boundary (e.g. in abc! but not in abcd)

perl\B

perl when not followed by a word
boundary (e.g. in perlert but not in perl stuff)

Examples of simple use in Perl statements

These examples use very simple regexps only. The
intent is just to show contexts where regexps might be
used, as well as the effect of some “flags” to matching
and replacements.
Note in particular that matching is
by default
case-sensitive
(Abc does not match abc unless
specified otherwise).

s/foo/bar/;
replaces the first occurrence of the exact character sequence
foo in the “current string” (in special
variable $_) by the character sequence bar;
for example, foolish bigfoot would become
barlish bigfoot

s/foo/bar/g;
replaces any occurrence of the exact character sequence
foo in the “current string”
by the character sequence bar;
for example, foolish bigfoot would become
barlish bigbart

s/foo/bar/gi;
replaces any occurrence of
foocase-insensitively in the “current string”
by the character sequence bar
(e.g. Foo and FOO get replaced by
bar too)

if(m/foo/)...
tests whether the current string contains the string foo

Date of creation: 2000-01-28.
Last revision: 2007-04-16.
Last modification: 2007-05-28.