Home arrow Perl Programming arrow Page 4 - Completing Regular Expression Basics

Creating Your Own Expressions - Perl

In this conclusion to a four-part series on parsing and regular expression basics in Perl, we finish our study of regular expressions; you'll even learn how to create your own. This article is excerpted from chapter one of the book Pro Perl Parsing, written by Christopher M. Frenz (Apress; ISBN: 1590595041).

TABLE OF CONTENTS:
  1. Completing Regular Expression Basics
  2. Universal Flags
  3. Subroutine-Based Usage
  4. Creating Your Own Expressions
By: Apress Publishing
Rating: starstarstarstarstar / 4
June 10, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The Regexp::Common module does not limit you to just the patterns that come with it. You also have the ability to create your own regular expressions, at run time, for use within the Regexp::Common module. For example, Regexp::Common does not yet support phone numbers, so letís begin to create aRegexp::Commonphone number entry (see Listing 1-10).

Listing 1-10. Creating Your Own Regexp::CommonExpression

#!/usr/bin/perl -w
use Regexp::Common qw /pattern/;

pattern name=>[qw(phone)],
    create=>q/(?k:\s?(\(\d{3}\))[-\s.](\d{3}[-.]\d{4}))/;

 

while(<>){
    /$RE{phone}{-keep}/ and print "$1\n";
}


Note  You may have noticed that the pattern contains the sequence of characters?k:in it. Under normal circumstances, capturing through parentheses is not preserved inRegexp::Common, since capturing parentheses are processed out. The?k:sequence tells the module not to process out these parentheses when the-keepflag is present. This is why you were able to print phone numbers by using$1in the previous example.


To begin, you must first tell Perl you are going to utilize the pattern subroutine of theRegexp::Commonmodule. Next, you must create anameargument that will specify the name of the pattern and any flags it may take. In this case, the pattern is namedphone. If you want to add additional names and/or flags, you can specify them as follows:

pattern name=[qw(phone book -flag)]

This specifies an entry of$RE{phone}{book}{-flag}.

After you name your pattern, you must next specify a value for thecreateargument. This argument is the only other required argument and can take either a string that is to be returned as a pattern (as previously) or a reference to a subroutine that will create the pattern. Also, two optional arguments also take subroutine references. These arguments arematchandsubs, and the provided subroutine will dictate what occurs when the methodsmatchandsubs, the matching and substitution methods (respectively), are called. Lastly, one more optional argument,version, can be assigned a Perl version number.

If the version of Perl is older than the supplied argument, the script will not run and a fatal error will be returned.

Summary

This chapter covered how to syntactically construct regular expressions and how you can call upon these expressions within your Perl scripts. Furthermore, I discussed the roles of the different quantifiers, assertions, and predefined subpatterns, as well as how best to debug regular expressions. Lastly, the chapter covered how the Perl module Regexp::Common works and how you can utilize it to locate elements of interest.

Now that you have an idea of how you can use regular expressions to match, and hence identify, portions of strings, you are more prepared to tackle the topics of tokens and grammars in greater depth as you delve into the next chapter. Chapter 2 will introduce you to the idea of generative grammars by covering the Chomsky hierarchy of grammars. The upcoming chapter will also demonstrate how you can use Perl code in conjunction with a grammar to generate sentences that comply with the rules specified in the grammar.  



 
 
>>> 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: