Regular Expressions in Perl

Our program demonstrates common regular expression tasks in Perl. Here’s the full source code:

use strict;
use warnings;
use feature 'say';

# This tests whether a pattern matches a string.
my $match = "peach" =~ /p([a-z]+)ch/;
say $match ? "true" : "false";

# We'll use this pattern for the following examples
my $pattern = qr/p([a-z]+)ch/;

# This is equivalent to the MatchString method in Go
say $pattern =~ /peach/ ? "true" : "false";

# This finds the match for the regexp.
if ("peach punch" =~ $pattern) {
    say $&;
}

# This also finds the first match but returns the
# start and end indexes for the match instead of the
# matching text.
if ("peach punch" =~ $pattern) {
    say "idx: [$-[0], $+[0]]";
}

# The capturing parentheses in the pattern allow us to extract
# submatches. This is similar to FindStringSubmatch in Go.
if ("peach punch" =~ $pattern) {
    say "[$&, $1]";
}

# To get all matches (like FindAllString in Go), we use the /g flag
my @matches = "peach punch pinch" =~ /$pattern/g;
say "[" . join(", ", @matches) . "]";

# To limit the number of matches, we can use a loop
my @limited_matches;
while ("peach punch pinch" =~ /$pattern/g) {
    push @limited_matches, $&;
    last if @limited_matches == 2;
}
say "[" . join(", ", @limited_matches) . "]";

# Perl's regexes work on both strings and binary data by default

# When creating global variables with regular expressions,
# you can use the qr// operator to pre-compile the regex
my $r = qr/p([a-z]+)ch/;
say "regexp: $r";

# The s/// operator can be used to replace subsets of strings
my $replaced = "a peach";
$replaced =~ s/$r/<fruit>/;
say $replaced;

# We can also use a function to transform matched text
my $transformed = "a peach";
$transformed =~ s/$r/uc($&)/e;
say $transformed;

To run the program, save it as regular-expressions.pl and use perl:

$ perl regular-expressions.pl
true
true
peach
idx: [0, 5]
[peach, ea]
[peach, punch, pinch]
[peach, punch]
regexp: (?^:p([a-z]+)ch)
a <fruit>
a PEACH

Perl has built-in support for regular expressions as a core language feature. Unlike Go, which uses a separate package for regex operations, Perl integrates regex syntax directly into the language.

The =~ operator is used for matching regexes against strings. The // delimiters define a regex pattern. The /g flag is used for global matching (finding all occurrences).

Perl uses special variables like $& (the entire match), $1, $2, etc. (capturing groups), and $-[0], $+[0] (start and end positions of the match) to provide information about matches.

The qr// operator pre-compiles a regex for efficiency when it’s used multiple times.

The s/// operator is used for substitutions, similar to ReplaceAllString in Go. The /e flag allows the replacement to be a Perl expression, which is evaluated for each match.

For a complete reference on Perl regular expressions, consult the official Perl documentation.