Regular expressions (regex) are a powerful tool for pattern matching and text manipulation. In Perl, regular expressions are integrated into the language, making it easy to perform complex text processing tasks. This section will cover the basics of regular expressions, their syntax, and practical examples to help you understand and use them effectively.

Key Concepts

  1. Pattern Matching: Identifying whether a string contains a specific pattern.
  2. Substitution: Replacing parts of a string that match a pattern.
  3. Modifiers: Altering the behavior of pattern matching.
  4. Special Characters: Characters with special meanings in regex.

Basic Syntax

Pattern Matching

In Perl, the =~ operator is used to apply a regex pattern to a string. The pattern is enclosed in forward slashes /.

my $string = "Hello, World!";
if ($string =~ /World/) {
    print "Pattern found!\n";
}

Substitution

The s/// operator is used for substitution. The pattern to be replaced is specified between the first pair of slashes, and the replacement string is specified between the second pair.

my $string = "Hello, World!";
$string =~ s/World/Perl/;
print "$string\n";  # Output: Hello, Perl!

Modifiers

Modifiers can be added after the closing slash to change the behavior of the regex. Common modifiers include:

  • i: Case-insensitive matching
  • g: Global matching (find all matches)
  • m: Multiline matching
my $string = "Hello, world!";
if ($string =~ /WORLD/i) {
    print "Pattern found!\n";  # Output: Pattern found!
}

Special Characters

Special characters have specific meanings in regex:

  • .: Matches any single character except newline
  • ^: Matches the start of a string
  • $: Matches the end of a string
  • *: Matches 0 or more occurrences of the preceding element
  • +: Matches 1 or more occurrences of the preceding element
  • ?: Matches 0 or 1 occurrence of the preceding element
  • \d: Matches any digit
  • \w: Matches any word character (alphanumeric plus underscore)
  • \s: Matches any whitespace character

Practical Examples

Example 1: Validating an Email Address

my $email = "[email protected]";
if ($email =~ /^[\w\.-]+@[\w\.-]+\.\w+$/) {
    print "Valid email address\n";
} else {
    print "Invalid email address\n";
}

Example 2: Extracting Numbers from a String

my $text = "The price is 100 dollars.";
if ($text =~ /(\d+)/) {
    print "Found number: $1\n";  # Output: Found number: 100
}

Example 3: Replacing Multiple Spaces with a Single Space

my $text = "This   is   a   test.";
$text =~ s/\s+/ /g;
print "$text\n";  # Output: This is a test.

Exercises

Exercise 1: Validate a Phone Number

Write a Perl script to validate a phone number in the format (123) 456-7890.

Solution:

my $phone = "(123) 456-7890";
if ($phone =~ /^\(\d{3}\) \d{3}-\d{4}$/) {
    print "Valid phone number\n";
} else {
    print "Invalid phone number\n";
}

Exercise 2: Extract Domain from an Email Address

Write a Perl script to extract the domain from an email address.

Solution:

my $email = "[email protected]";
if ($email =~ /@([\w\.-]+)/) {
    print "Domain: $1\n";  # Output: Domain: example.com
}

Exercise 3: Find All Words in a String

Write a Perl script to find all words in a given string.

Solution:

my $text = "This is a test.";
my @words = $text =~ /\b\w+\b/g;
print "Words: @words\n";  # Output: Words: This is a test

Common Mistakes and Tips

  • Forgetting to escape special characters: Remember to escape characters like . and * if you want to match them literally.
  • Using greedy quantifiers: By default, quantifiers like * and + are greedy. Use *? and +? for non-greedy matching.
  • Not using anchors: Use ^ and $ to match the start and end of strings, respectively, to avoid partial matches.

Conclusion

Regular expressions are a powerful feature in Perl for text processing. By understanding the basic syntax, special characters, and modifiers, you can perform complex pattern matching and text manipulation tasks. Practice with the provided examples and exercises to reinforce your understanding and become proficient in using regular expressions in Perl.

© Copyright 2024. All rights reserved