regex beginner

How to perform a simple regex match in Perl?

Question

How to perform a simple regex match in Perl?

Performing a simple regular expression (regex) match in Perl is straightforward and is one of the language's core strengths. Perl’s regex syntax is embedded directly into the language using the m// operator (where the "m" stands for "match"). Typically, you use it within an if statement or as an expression to test whether a pattern matches a string.

Basic Regex Match Syntax

The common form is:


$string =~ m/pattern/;
  • $string is the variable holding the string you want to test.
  • =~ is the binding operator that applies the regex to the string on its left.
  • pattern is your regular expression, enclosed between delimiters such as slashes /.

For simple matches, you can omit the m and write it as:


$string =~ /pattern/;

How it Works

  • The match operator returns a true value if the regex finds a match, false if it doesn't.
  • Commonly used in if statements to conditionally respond to pattern matches.
  • Parentheses () can capture submatches into variables $1, $2, etc.

Example: Match a Pattern in a String


#!/usr/bin/perl
use strict;
use warnings;

my $text = "Hello, Perl regex!";

if ($text =~ /Perl/) {
    print "Found the word 'Perl' in text.\n";
} else {
    print "Did not find 'Perl' in text.\n";
}

In this example:

  • We declare a string $text.
  • The regex /Perl/ looks for the substring "Perl" inside $text.
  • If the match succeeds, it prints confirmation; otherwise, a negative message.

Capturing Submatches

You can parenthesize parts of the pattern to capture them:


my $email = "user@example.com";

if ($email =~ /(\w+)@(\w+\.\w+)/) {
    print "Username: $1\n";
    print "Domain: $2\n";
} else {
    print "Not a valid email format.\n";
}

Here, $1 and $2 contain matched portions of the string based on the parentheses.

Important Perl Regex Concepts

  • Sigils: In Perl, variables start with sigils that denote context: $ for scalars (strings, numbers), @ for arrays, and % for hashes. Regex matches operate in scalar context typically.
  • Context Sensitivity: When used in scalar context (like inside an if condition), regex returns true/false. In list context, it can return captured matches.
  • TMTOWTDI: “There's more than one way to do it!” Perl regex allows many delimiters (//, m||, m{} , etc.) and options like i for case-insensitive, g for global matching.
  • Versions: Basic regex features are available in all Perl 5 versions. Modern Perl (5.10+) supports enhanced regex features like named captures and \K, but basic matching works everywhere.

Common Pitfalls

  • Forgetting the =~ operator and trying if (/pattern/) alone will blindly match against $_ (default variable) which may cause confusion.
  • Not using use strict; and use warnings;, which can help catch mistakes around variable use.
  • Confusing match operators with substitution s/// or transliteration tr///.
  • Captured variables $1, $2, etc., only set when a match succeeds.

With these basics and the code example, you can perform simple regex matches in Perl confidently!

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 5ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
(empty)
STDERR
(empty)

Was this helpful?

Related Questions