Coding the AND Operator in Perl Regular Expression
Advanced Perl Regular Expressions – Part 5
Foreword: In this part of the series, I explain how to code the AND operator for a Perl regular expression.
By: Chrysanthus Date Published: 2 Apr 2016
Introduction
Motivation
In this section I explain what motivated me to produce this article. As of today, if you want to match this or that in a subject string, you have to produce a regex like:
/this|that/
where | is the regex OR operator. Is there an equivalent operator for AND? - No. If you want it, you have to code it. In this article I give you a simple solution that works with all systems.
The Solution
Above, you have two alternatives (this or that). You can actually have more than two alternatives. AND means all alternatives must be present in the subject string. The solution is surprisingly simple: Just make sure each of the alternatives is found in the subject, with a foreach loop.
Algorithm of Solution
- Place all the alternatives in an array.
- Verify if each alternative is in the subject with regular expression technique in the foreach loop.
- Any alternative not in the subject means false.
If all alternatives are in the subject, that means true. So, that is my solution on how to code the AND operator for Perl regex, until Perl gives us an operator for AND.
Code Example
The following code illustrates this with two code segments. The first segment of interest returns TRUE and the second one returns FALSE. The alternatives are: “pencil”, “book” and “pen”. Read and try the code:
use strict;
my $subject0 = "This is a pencil. Is that a book? I use a pen.";
my @arr0 = ("pencil", "book", "pen");
my $bool0 = 0;
foreach my $var (@arr0)
{
if ($subject0 =~ /$var/)
{
$bool0 = 1;
}
else
{
$bool0 = 0;
last;
}
}
print $bool0, "\n";
my $subject1 = "This is a pencil. I use a pen.";
my @arr1 = ("pencil", "book", "pen");
my $bool1 = 0;
foreach my $var (@arr1)
{
if ($subject1 =~ /$var/)
{
$bool1 = 1;
}
else
{
$bool1 = 0;
last;
}
}
print $bool1, "\n";
Each of the code segments simulates the AND operator. In your own project, you can vary the AND code, but the algorithm stays the same.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT