Home arrow Perl Programming arrow Page 4 - Parsing and Regular Expression Basics

Pattern Matching - Perl

Parsing helps us to extract the information we need from the mounds of data available. Regular expressions assist us in the hunt. This four-part article series will show you how to use Perl with regular expressions to accomplish your parsing tasks. It is excerpted from chapter one of the book Pro Perl Parsing, written by Christopher M. Frenz (Apress; ISBN: 1590595041).

TABLE OF CONTENTS:
  1. Parsing and Regular Expression Basics
  2. Parsing and Lexing
  3. Using Regular Expressions
  4. Pattern Matching
By: Apress Publishing
Rating: starstarstarstarstar / 1
May 20, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now that you know how the regular expression engine functions, letís look at how you can invoke this engine to perform pattern matches within Perl code. To perform pattern matches, you need to first acquaint yourself with the binding operators, =~ and !~. The string you seek to bind (match) goes on the left, and the operator that it is going to be bound to goes on the right. You can employ three types of operators on the right side of this statement. The first is the pattern match operator,m//, or simply//(themis implied and can be left out), which will test to see if the string value matches the supplied expression, such as123matching/123/, as shown in Listing 1-2. The remaining two ares///andtr///, which will allow for substitution and transliteration, respectively. For now, I will focus solely on matching and discuss the other two alternatives later. When using=~, a value will be returned from this operation that indicates whether the regular expression operator was able to successfully match the string. The!~functions in an identical manner, but it checks to see if the string is unable to match the specified operator. Therefore, if a=~operation returns that a match was successful, the corresponding!~operation will not return a successful result, and vice versa. Letís examine this a little closer by considering the simple Perl script in Listing 1-2.

Listing 1-2.  Performing Some Basic Pattern Matching 

#!/usr/bin/perl

$string1="123";
$string2="ABC";
$pattern1="123";

if($string1=~/$pattern1/){
    print "123=123\n";
}

if($string2!~/123/){
    print "ABC does not match /123/\n";
}

if("234"=~/$pattern1|ABC/){
    print "This is 123 or ABC\n";
}
else{print "This is neither 123 nor ABC";}

The script begins by declaring three different scalar variables; the first two hold string values that will be matched against various regular expressions, and the third serves as storage for a regular expression pattern. Next you use a series of conditional statements to evaluate the strings against a series of regular expressions. In the first conditional, the value stored in$string1matches the pattern stored in$pattern1, so theprintstatement is able to successfully execute. In the next conditional,$string2does not match the supplied pattern, but the operation was conducted using the!~operator, which tests for mismatches, and thus thisprintstatement can also execute. The third conditional does not return a match, since the string234 does not match either alternative in the regular expression. Accordingly, in this case theprintstatement of theelsecondition will instead execute. A quick look at the output of this script confirms that the observed behavior is in agreement with what was anticipated:

123=123
ABC does not match /123/
This is neither 123 nor ABC

Operations similar to these serve as the basis of pattern matching in Perl. However, the basic types of patterns you have learned to create so far have only limited usefulness. To gain more robust pattern matching capabilities, you will now build on these basic concepts by further exploring the richness of the Perl regular expression syntax.

Please check back next week for the continuation of this article.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: