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

Universal Flags - 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).

  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



As you saw in the previous sections, many of the base modules have their own flags, which can be used to further refine the pattern your regular expression will match (see Table 1-8). You can use two additional flags, however, with almost all base modules. These flags are the -i flag and the -keep flag. The -i flag makes the regular expression insensitive to alphabetic case so the expression can match both lowercase and capital letters. You can use the-keepflag for pattern capturing. If you specify-keep, the entire match to the pattern is generally stored in$1. In many cases,$2,$3, and other variables are also set, but these are set in a module-specific manner.


Table 1-8. Regexp::CommonFlags

Flag Use Module(s)
-sep Specifies a separator Net and List
-lastsep Specifies the last separator of a list List
-base For numbers, makes the base something other than base 10 Number
-radix Makes a decimal point something other than. Number
-places Specifies the number of places after a decimal point Number
-group For numbers, specifies the number of digits that should be present between separators Numbers
-expon Specifies the exponent pattern Numbers
-i Makes the regular expressions case insensitive All
-keep Enables substring capturing All



Standard Usage

You can utilize the patterns located in the module in your source code in a couple of ways. The first of these ways is referred to as the standard usage method and has a syntax similar to some of the regular expressions you have already seen in that the expression is placed between the // operator. The only difference is that rather than placing your own regular expression between //, you place one of the modules hash values. Consider the following segment of text:

Bob said "Hello". James
responded "Hi, how are you".
Bob replied "Fine and you".

Now letís save this text to a file and execute the Perl code shown in Listing 1-8, making sure to pass the name of the file you just saved as an argument.

Listing 1-8. Pulling Quotes Out of a Document

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

    and print "$1\n";

This short piece of code will read through the contents of the file and identify all the quotes present in the text file. Since you also specified the-keepflag, you are able to capture the quotes and print them. Thus, the output for this script should be similar to the following:

"Hi, how are you"
"Fine and you"

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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