Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Regular expressions (regex or regexp) are one of the most powerful features in Perl. They allow you to match, extract, replace, or even split text based on patterns. Let's dive into a comprehensive tutorial on regular expressions in Perl:
Use =~
to match a string against a regex:
my $str = "Hello, world!"; if ($str =~ /world/) { print "Matched!\n"; }
*
: Match 0 or more times.+
: Match 1 or more times.?
: Match 0 or 1 time.{n}
: Match exactly n times.{n,}
: Match n or more times.{n,m}
: Match between n and m times.my $str = "wooooow"; if ($str =~ /wo{5}w/) { print "5 'o' characters matched!\n"; }
Character classes match one character out of a set:
[abc]
: Matches a single character which can be a
, b
, or c
.[a-z]
: Matches any single lowercase letter.[A-Z]
: Matches any single uppercase letter.[^a-z]
: Matches any character except lowercase letters.\d
: Matches a digit ([0-9]
).\D
: Matches any non-digit.\w
: Matches a word character (equivalent to [a-zA-Z0-9_]
).\W
: Matches any non-word character.\s
: Matches any whitespace character (spaces, tabs, newlines).\S
: Matches any non-whitespace character.^
: Matches the start of the string.$
: Matches the end of the string.if ($str =~ /^Hello/) { ... } # Matches "Hello" at the beginning.
You can group parts of your pattern and capture matched content:
my $date = "2021-09-04"; if ($date =~ /(\d{4})-(\d{2})-(\d{2})/) { print "Year: $1, Month: $2, Day: $3\n"; }
i
: Case-insensitive match.m
: Treat the string as multiple lines.s
: Treat the string as a single line (so .
matches even \n
).x
: Extended mode (allows you to use whitespace and comments in the regex).Replace parts of the string:
my $str = "blue sky"; $str =~ s/blue/green/; print $str; # Outputs "green sky"
Match all occurrences in a string:
my $str = "cats and dogs"; while ($str =~ /cat|dog/g) { print "Found: $&\n"; }
Use (?: ... )
for non-capturing groups:
if ($str =~ /(?:cat|dog)s/) { print "Found plural: $&\n"; }
(?=...)
(?!...)
(?<=...)
(?<!...)
if ($str =~ /cat(?=s)/) { print "Found 'cat' followed by 's'\n"; }
You can split strings using regex:
my $data = "John:25:USA"; my @fields = split /:/, $data;
Regular expressions in Perl are a vast topic. This tutorial provides an overview of the basics and some advanced concepts. Mastery of regex can save a lot of time and code, making it a valuable skill for any Perl programmer. To dive deeper, consider studying the Perl regex documentation (perldoc perlre
).
Introduction to regex in Perl:
my $string = "The quick brown fox jumps over the lazy dog."; if ($string =~ /quick/) { print "Match found!\n"; }
Perl regex pattern examples:
my $email = "user@example.com"; if ($email =~ /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/) { print "Valid email address!\n"; }
Using regular expressions for pattern matching in Perl:
=~
operator to apply regex patterns and perform pattern matching on strings.my $sentence = "The cat in the hat."; if ($sentence =~ /cat/) { print "Found 'cat' in the sentence!\n"; }
Quantifiers and modifiers in Perl regex:
my $numbers = "123 456 789"; if ($numbers =~ /\d{3}/) { print "Three consecutive digits found!\n"; }
Perl regex character classes:
my $code = "A1b C3"; if ($code =~ /[A-Za-z]\d/) { print "Alphabetic character followed by a digit found!\n"; }
Lookahead and lookbehind in Perl regex:
my $price = "$100"; if ($price =~ /\d+(?=\$)/) { print "Price found without including the dollar sign!\n"; }
Substitution and matching with regex in Perl:
my $text = "Replace me!"; $text =~ s/Replace/Updated/; print "$text\n"; # Output: "Updated me!"
Advanced regex techniques in Perl:
my $html = "<p>Hello, <b>world</b>!</p>"; if ($html =~ m{<b>(.*?)</b>}) { my $bold_text = $1; print "Bold text: $bold_text\n"; }