Regular expressions (regex or regexp) are powerful patterns used to match and manipulate text. Perl has robust support for regular expressions, allowing developers to perform complex pattern-matching and text-processing operations efficiently. Here’s a comprehensive explanation of regular expressions in Perl, along with examples:

Basic Syntax:
In Perl, regular expressions are denoted by enclosing patterns in forward slashes (/pattern/). They can also be used as part of the m// (match) operator or the s/// (substitution) operator.

Character Classes:

  • Literal Characters: Match literal characters in the text.
    perl my $string = "hello"; if ($string =~ /hello/) { print "Match found\n"; # Output: Match found }
  • Metacharacters: Special characters with predefined meanings, such as . (any character), \d (digit), \w (word character), \s (whitespace), ^ (start of line), and $ (end of line).
    perl my $string = "1234"; if ($string =~ /^\d+$/) { print "Match found\n"; # Output: Match found }

Quantifiers:

  • *, +, ?: Match zero or more, one or more, or zero or one occurrences of the preceding character or group.
    perl my $string = "aaaa"; if ($string =~ /a+/) { print "Match found\n"; # Output: Match found }
  • {n}, {n,}, {n,m}: Match exactly n, n or more, or between n and m occurrences of the preceding character or group.
    perl my $string = "abbbbb"; if ($string =~ /b{2,4}/) { print "Match found\n"; # Output: Match found }

Anchors and Boundaries:

  • ^, $: Match the start and end of a line, respectively.
    perl my $string = "hello"; if ($string =~ /^h/) { print "Match found\n"; # Output: Match found }
  • \b, \B: Match word boundaries (start or end of a word) or non-word boundaries.
    perl my $string = "hello world"; if ($string =~ /\bhello\b/) { print "Match found\n"; # Output: Match found }

Grouping and Capturing:

  • (...): Group characters together for applying quantifiers or alternation.
    perl my $string = "hello world"; if ($string =~ /(hello) (world)/) { print "$1, $2\n"; # Output: hello, world }

Alternation:

  • |: Match one of several alternatives.
    perl my $string = "cat"; if ($string =~ /cat|dog/) { print "Match found\n"; # Output: Match found }

Modifiers:

  • i: Case-insensitive matching.
    perl my $string = "Hello"; if ($string =~ /hello/i) { print "Match found\n"; # Output: Match found }

Using Regular Expressions in Perl:

  • Matching: Use the =~ operator to match a string against a regular expression.
  • Substitution: Use the s/// operator to perform substitutions based on a regular expression.
  • Matching Groups: Use parentheses to capture substrings that match specific patterns. Example:
   my $string = "The quick brown fox jumps over the lazy dog";
   if ($string =~ /quick (brown) fox/) {
       print "Match found: $1\n";  # Output: Match found: brown
   }

Regular expressions in Perl provide a powerful and flexible way to search for, extract, and manipulate text data. By mastering regular expressions, developers can perform complex text-processing tasks efficiently and effectively in Perl applications.