Perl Regular Expressions Guide — Pattern Matching and Text Processing
In this tutorial, you will learn about Perl Regular Expressions Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Perl regular expressions are built into the language syntax with the =~ binding operator -- supporting pattern matching (m//), substitution (s///), transliteration (tr///), and advanced features like captures, lookahead, and global matching.
Basic Matching
use strict;
use warnings;
my $text = "The quick brown fox jumps over the lazy dog.";
# Simple match
if ($text =~ /quick/) {
print "Found 'quick'\n";
}
# Case-insensitive
if ($text =~ /THE/i) {
print "Case-insensitive match\n";
}
# Match anywhere (default behavior)
Captures
my $text = "Name: Alice, Age: 30, City: New York";
# Parentheses capture
if ($text =~ /Name: (\w+), Age: (\d+)/) {
print "Name: $1, Age: $2\n"; # Alice, 30
}
# Named captures (Perl 5.10+)
if ($text =~ /Name: (?<name>\w+), Age: (?<age>\d+)/) {
print "Name: $+{name}, Age: $+{age}\n";
}
Substitution
my $s = "Hello world, hello Perl";
# Simple substitution
(my $modified = $s) =~ s/hello/hi/;
print "$modified\n"; # Hi world, hello Perl
# Global substitution
(my $global = $s) =~ s/hello/hi/gi; # case-insensitive
print "$global\n"; # Hi world, hi Perl
# In-place modification
$s =~ s/Perl/Lua/;
Regex Modifiers
# /i - case insensitive
# /g - global (all occurrences)
# /m - multiline (^ and $ match line boundaries)
# /s - single line (. matches newline)
# /x - extended (allow whitespace and comments)
# /o - compile once
my $text = "Line 1\nLine 2\nLine 3\n";
my @matches = $text =~ /^Line \d+$/gm; # all 3 lines
Advanced Patterns
my $text = "Email: user@example.com, Phone: 555-1234";
# Alternation
if ($text =~ /Email|Phone/) { }
# Character classes
if ($text =~ /[A-Z]/) {} # uppercase letter
if ($text =~ /[0-9]/) {} # digit
if ($text =~ /[^0-9]/) {} # not a digit
# Quantifiers
# * - zero or more
# + - one or more
# ? - zero or one
# {n} - exactly n
# {n,} - n or more
# Non-greedy
my $html = "<b>Bold</b> and <i>italic</i>";
my ($greedy) = $html =~ /<b>(.*?)<\/b>/; # non-greedy
Common Mistakes
1. Forgetting =~ binding
$str =~ /pattern/ -- without =~, the pattern matches against $_.
2. Not escaping special characters
Match . with \., * with \*, ? with \?.
3. Greedy vs non-greedy
Use *? or +? for minimal match. Default is greedy (maximum match).
Practice Questions
1. How do you do a case-insensitive match?
Add the /i modifier: $text =~ /pattern/i.
2. What do $1, $2 capture?
They capture text matched by parenthesized groups () in the last successful match.
3. How do you replace all occurrences?
Use the /g modifier with s///: $text =~ s/old/new/g.
FAQ
{{< faq question="What is the difference between m// and qr//?" >}}
m// performs matching immediately. qr// compiles a regex pattern for later use: my $re = qr/pattern/; $text =~ $re;.
{{< /faq >}}
{{< faq question="How do I match across multiple lines?" >}}
Use the /m modifier so ^ and $ match start/end of each line. Use /s so . matches newline.
{{< /faq >}}
{{< faq question="What is the difference between \w and [[:word:]]?" >}}
\w matches [a-zA-Z0-9_]. [[:word:]] matches Unicode word characters when the /u modifier is used.
{{< /faq >}}
What's Next
Now learn about Perl subroutines.
| Topic | Description | Link |
|---|---|---|
| Subroutines | Functions and parameters | {{< ref "07-subroutines" >}} |
| File I/O | Reading and writing files | {{< ref "08-file-io" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro