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) are a powerful tool for text processing, and Perl is known for its strong regex capabilities. In this tutorial, we'll demonstrate how to search within a file using regex in Perl.
Perl's regex capabilities make it easy to search for patterns within a file. The primary operator for regex matching in Perl is =~
.
To search a file using regex, you first need to read the file:
open my $fh, '<', 'filename.txt' or die "Could not open file: $!"; while (my $line = <$fh>) { # Do something with $line } close $fh;
Suppose we want to search for the word "apple" in each line of a file:
open my $fh, '<', 'filename.txt' or die "Could not open file: $!"; while (my $line = <$fh>) { if ($line =~ /apple/) { print "Found apple in: $line"; } } close $fh;
Modifiers can change how the regex is applied:
i
: Case-insensitive matchm
: Treat string as multiple lines (changes the behavior of ^
and $
)s
: Treat string as single line (dot .
matches newline)# Case-insensitive search for "apple" if ($line =~ /apple/i) { ... }
You can capture portions of the matched text by using parentheses:
if ($line =~ /(apple)/) { print "Found: $1\n"; # $1 contains the first captured group }
You can use alternation to search for multiple patterns:
if ($line =~ /apple|orange|banana/) { print "Found a fruit in: $line"; }
Regular expressions can be used to extract specific information from a line:
# Extracting date in the format YYYY-MM-DD if ($line =~ /(\d{4})-(\d{2})-(\d{2})/) { print "Year: $1, Month: $2, Day: $3\n"; }
Regular expressions offer a powerful way to search and manipulate text in Perl. Combining regex with Perl's file-handling capabilities allows you to efficiently search within files, extract useful information, and perform complex text processing tasks.
Perl regex search in file example:
my $pattern = qr/keyword/; open my $file, '<', 'example.txt' or die "Could not open file: $!"; while (<$file>) { print if /$pattern/; } close $file;
Using regular expressions for file searching in Perl:
my $pattern = qr/\d{3}-\d{2}-\d{4}/; # Searching for SSN-like patterns open my $file, '<', 'data.txt' or die "Could not open file: $!"; while (<$file>) { print if /$pattern/; } close $file;
Search and match patterns in a file with Perl:
m//
operator to match patterns in each line of a file.my $pattern = qr/Perl/; open my $file, '<', 'code.txt' or die "Could not open file: $!"; while (<$file>) { print if m/$pattern/; } close $file;
Perl file search and replace with regex:
my $search_pattern = qr/old_string/; my $replace_string = "new_string"; local @ARGV = ('file.txt'); local $^I = '.bak'; while (<>) { s/$search_pattern/$replace_string/g; print; }
Searching for multiple patterns in a file using Perl regex:
my @patterns = (qr/error/, qr/failure/); open my $file, '<', 'log.txt' or die "Could not open file: $!"; while (<$file>) { print if any { /$_/ } @patterns; } close $file;
Recursive file searching with Perl regex:
use File::Find; my $pattern = qr/\.txt$/; # Match files with a .txt extension find(sub { print "$File::Find::name\n" if /$pattern/; }, '/path/to/search');
Case-insensitive file search in Perl:
my $pattern = qr/perl/i; # Case-insensitive search for 'perl' open my $file, '<', 'text.txt' or die "Could not open file: $!"; while (<$file>) { print if /$pattern/; } close $file;
Filtering lines in a file with regex in Perl:
my $pattern = qr/^DEBUG/; # Lines starting with 'DEBUG' open my $file, '<', 'logfile.txt' or die "Could not open file: $!"; while (<$file>) { print if /$pattern/; } close $file;