Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 43 ยท Regular Expressions

Regular Expressions Basics

Start with Perl regular expressions: the m// operator, the =~ binding, metacharacters, character classes, quantifiers, anchors, alternation and lazy matching.

A regular expression (regex) is a compact pattern that describes text. You use it to ask questions such as "does this line contain a date?", "which words start with a capital?" or "is this input a valid code?". Regexes are Perl's most famous feature: the pattern syntax is built into the language itself, so matching text takes very little code.

This lesson covers the building blocks: the match operator, character classes, quantifiers, anchors, alternation and the difference between greedy and lazy matching. Later lessons add captures, substitution and advanced tricks.

Perl is not installed on this site, so the Perl code below is read-only, and its output comes from a real Perl 5.42 run. The interactive examples use Python's re module as a stand-in. For the basics in this lesson, Perl and Python patterns are written the same way, but the surrounding syntax differs, and the lesson says where.

Parts of a pattern

Literals Plain characters match themselves.
Classes One character from a set.
Quantifiers How many times to repeat.
Anchors Where in the string.
1 / 2

The match operator m// and =~

The pattern goes between slashes. With =~ you bind it to the string you want to test. The result is true when the pattern is found anywhere in the string, and false otherwise.

PERL
use v5.36;

my $s = "The cat sat on the mat";

say "has cat" if $s =~ /cat/;
say "no dog"  if $s !~ /dog/;       # !~ means "does not match"
say "Cat? ", ($s =~ /Cat/ ? "yes" : "no");
say "ignoring case" if $s =~ /CAT/i;
TEXT
has cat
no dog
Cat? no
ignoring case

Matching is case-sensitive unless you add the /i modifier. If you leave off =~, Perl matches against the default variable $_, as in say "found" if /world/;. The m in m// can be left out when you use slashes, but writing it lets you pick other delimiters, which helps when the pattern contains many slashes: m{/usr/local}.

Here is the same idea in Python. Perl's =~ becomes a call to re.search, which returns a match object, or None when there is no match.

Python Stand-in: does a pattern match?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Metacharacters

Most characters match themselves. A few have a special meaning and are called metacharacters:

TEXT
.  ^  $  *  +  ?  (  )  [  ]  {  }  |  \

To match one of them literally, put a backslash in front of it: \. matches an actual period. Inside a pattern, . (an unescaped dot) matches any one character except a newline.

PERL
say join ",", "cat cot cut c t" =~ /c.t/g;
say "a.b" =~ /a\.b/ ? "escaped dot matches" : "no";
say "axb" =~ /a\.b/ ? "matches" : "escaped dot does not match x";
TEXT
cat,cot,cut,c t
escaped dot matches
escaped dot does not match x

The /g modifier asks for all matches. In list context, the match returns every piece of text that matched, so c t (with a space) is found because the dot accepts a space. Modifiers are covered in Captures and Modifiers.

When the pattern comes from a variable, wrap it in \Q...\E to turn every metacharacter into plain text, so user input cannot change the meaning of your pattern:

PERL
my $word = "a.b";
say "axb" =~ /\Q$word\E/ ? "matches" : "quoted pattern does not match axb";
TEXT
quoted pattern does not match axb

Character classes

A character class in square brackets matches one character from a set. A range uses a hyphen, and a caret at the start negates the class.

PERL
say join ",", "cat bat rat hat" =~ /[cbr]at/g;
say join ",", "a1 b2 c3 d" =~ /[^\d\s]\d?/g;
TEXT
cat,bat,rat
a1,b2,c3,d

The first line finds words that begin with c, b or r. The second finds one character that is not a digit and not whitespace, followed by an optional digit. Perl has short names for the common classes:

Shorthand Matches Opposite
\d a digit, 0 to 9 \D
\w a letter, digit or underscore \W
\s whitespace (space, tab, newline) \S
. any character except newline n/a
PERL
say join ",", "a1b22c333" =~ /\d+/g;
say join ",", "red green  blue" =~ /\w+/g;
say join "|", "x  y\tz" =~ /\S+/g;
TEXT
1,22,333
red,green,blue
x|y|z

Inside a class, most metacharacters lose their special meaning, so [.] matches a literal dot. You can also use POSIX names such as [[:alpha:]] for letters. In current Perl versions, \d and \w also match digits and letters from other scripts in Unicode strings, a detail that matters when you process international text.

Quantifiers

A quantifier says how many times the thing before it may repeat.

Quantifier Meaning
* zero or more
+ one or more
? zero or one (optional)
exactly three
{2,4} two to four
{2,} two or more
PERL
say join ",", "2024-03-15 and 1999-12-31" =~ /\d{4}-\d{2}-\d{2}/g;
say join ",", "color colour" =~ /colou?r/g;
say join ",", "aaa" =~ /a{2}/g;
TEXT
2024-03-15,1999-12-31
color,colour
aa

The last example shows that a{2} consumes characters from left to right and cannot reuse them, so aaa yields one aa, not two. A quantifier applies to the single item just before it: one character, a class, or a group in parentheses. To repeat several characters, use a group: (?:abc)+ matches abc, abcabc and so on. The ?: makes the group non-capturing, a detail explained in the next lesson.

Anchors and boundaries

Anchors match a position instead of a character.

  • ^ matches at the start of the string.
  • $ matches at the end (or just before a final newline).
  • \b matches a word boundary, the edge between a word character and anything else.
PERL
say "start" if "hello" =~ /^hel/;
say "end"   if "hello" =~ /llo$/;
say "exact" if "hello" =~ /^hello$/;

my $text = "cat concat cats";
my @loose = $text =~ /cat/g;
my @whole = $text =~ /\bcat\b/g;
say scalar(@loose), " ", scalar(@whole);
TEXT
start
end
exact
3 1

Without anchors, /cat/ matches inside concat and cats, which is usually not what you want. The \bcat\b version finds only the standalone word. When you validate input, always anchor both ends (^...$), or a string with one good piece and extra junk would pass.

Alternation and grouping

The vertical bar | means "or". It splits the pattern into alternatives, and parentheses limit how far it reaches:

PERL
say join ",", "cat dog bird" =~ /cat|bird/g;
say join ",", "foo.txt bar.pl baz.md" =~ /\w+\.(?:txt|pl)/g;
TEXT
cat,bird
foo.txt,bar.pl

Without the parentheses, \w+\.txt|pl would mean "a word, a dot and txt, or pl". Grouping is the fix.

Here is a Python stand-in that uses all the pieces from this section. The patterns are written the same way as in Perl; only the call differs: re.findall plays the role of a list-context /g match.

Python Stand-in: classes, quantifiers and anchors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Greedy and lazy matching

Quantifiers are greedy by default: they take as much text as they can while still letting the whole pattern match. Add a question mark after a quantifier to make it lazy, so it takes as little as possible.

PERL
my $html = "<b>bold</b> and <i>it</i>";

say $html =~ /<.+>/  ? $& : "";    # greedy
say $html =~ /<.+?>/ ? $& : "";    # lazy
TEXT
<b>bold</b> and <i>it</i>
<b>

The greedy .+ ran to the last > in the string, so the match swallowed everything. The lazy .+? stopped at the first >. The special variable $& holds the text of the whole last match. Another good fix is to describe what you want exactly, as in <[^>]+>, which means "an opening bracket, then non-bracket characters, then a closing bracket". That version is both clearer and faster than lazy matching.

Python Stand-in: greedy versus lazy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Regex building blocks you will reuse

A few patterns appear again and again:

  • ^\s*$ matches an empty or blank line.
  • \d+ matches a whole number (digits only).
  • ^\w+$ matches a single word with no spaces.
  • \.txt$ matches a file name that ends in .txt.

You can store a pattern in a variable with qr//, which compiles it once, and then use the variable in matches: my $num = qr/\d+/; say "has number" if "abc123" =~ $num;. The qr operator comes back in Advanced Regex.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which operator binds a pattern to a string in Perl?

    The binding operator =~ tests a string against a pattern. Its negation is !~.
  2. 2. What does /colou?r/ match?

    The ? makes the u optional, so the pattern matches color and colour.
  3. 3. Which pattern matches a standalone word cat, not concat or cats?

    \b is a word boundary, so \bcat\b matches cat only when it is not part of a longer word.
  4. 4. Why does /<.+>/ on <b>x</b> match the whole string?

    A greedy .+ runs to the last > it can still match. Adding ? (.+?) makes it lazy so it stops at the first one.
  5. 5. How do you insert a variable into a pattern so its characters are matched literally?

    \Q...\E quotes all metacharacters, so a value like a.b matches only the text a.b.

Summary

  • Match with $string =~ /pattern/; use !~ for "does not match", and /i to ignore case.
  • Metacharacters such as ., * and ? have special meanings; escape them with a backslash or quote a variable with \Q...\E.
  • Classes ([abc], \d, \w, \s) match one character; quantifiers (*, +, ?, {n,m}) repeat the item before them.
  • Anchors ^, $ and \b match positions; always anchor input validation at both ends.
  • Quantifiers are greedy by default; add ? for lazy, or describe the text exactly with a negated class.

Next, pull pieces of text out of a match in Captures and Modifiers.