Section 8.2. Option Modifiers

8.2. Option Modifiers

Several option modifier letters, sometimes called flags, may be appended as a group right after the ending delimiter of a regular expression to change its behavior from the default.

8.2.1. Case-Insensitive Matching with /i

To make a case-insensitive pattern match, so you can match FRED as easily as fred or Fred, use the /i modifier:

     print "Would you like to play a game? ";     chomp($_ = <STDIN>);     if (/yes/i) {  # case-insensitive match       print "In that case, I recommend that you go bowling.\n";     }

8.2.2. Matching Any Character with /s

By default, the dot (.) doesn't match newline, and this makes sense for most "look within a single line" patterns. If you have newlines in your strings and want the dot to be able to match them, the /s modifier will do the job. It changes every dot[*] in the pattern to act as the character class [\d\D] does, which is to match any character, even if it is a newline. You have to have a string with newlines for this to make a difference:

[*] If you wish to change some of them, but not all, you'll probably want to replace those few with [\d\D].

     $_ = "I saw Barney\ndown at the bowling alley\nwith Fred\nlast night.\n";     if (/Barney.*Fred/s) {       print "That string mentions Fred after Barney!\n";     }

Without the /s modifier, that match would fail since the two names aren't on the same line.

8.2.3. Adding Whitespace with /x

The /x modifier allows you to add arbitrary whitespace to a pattern to make it easier to read.

     /-?\d+\.?\d*/         # what is this doing?     / -? \d+ \.? \d* /x   # a little better

Since /x allows whitespace inside the pattern, a literal space or tab character within the pattern is ignored. You could use a backslashed space or \t (among many other ways) to match these, but using \s (or \s* or \s+) is more common when you want to match whitespace anyway.

In Perl, comments may be included as part of the whitespace, so with /x, we can put comments into that pattern to tell what it's doing:

     /       -?      # an optional minus sign       \d+     # one or more digits before the decimal point       \.?     # an optional decimal point       \d*     # some optional digits after the decimal point     /x        # end of pattern

Since the pound sign indicates the start of a comment, use \# or [#] in the rare case that you need to match a pound sign. And be careful not to include the closing delimiter inside the comments, or that will prematurely terminate the pattern.

8.2.4. Combining Option Modifiers

If you have more than one option modifier to use on the same pattern, they may be used one after the other. Their order isn't significant:

     if (/barney.*fred/is) {  # both /i and /s       print "That string mentions Fred after Barney!\n";     }

For a more expanded version with comments:

     if (m{       barney # the little guy       .*     # anything in between       fred   # the loud guy     }six) {  # all three of /s and /i and /x       print "That string mentions Fred after Barney!\n";     }

Note the shift to curly braces here for the delimiters as well, allowing programmer-style editors to easily bounce from the beginning to the ending of the regular expression.

8.2.5. Other Options

Many other option modifiers are available. We'll cover those as we get to them, or you can read about them in the perlop manpage and in the descriptions of m// and the other regular expression operators that you'll see later in this chapter.

Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: