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).
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;
while(<>){ /$RE{delimited}{-delim=>'"'}{-keep}/ 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: