Regular Expressions   «Prev  Next»
Lesson 5 Pattern-matching modifiers
Objective Write a program that uses the m// operator and appropriate modifiers

Perl Pattern-matching Modifiers

Write a program that uses the m// operator and appropriate modifiers to print out all of the tags in a HTML file.
A modifier is a letter that's used at the end of the operator to modify the behavior of the match. The m// operator allows the use of several modifiers.

match globally; that is, find all occurrences

Perl g Modifier

The global modifier (g) is used to perform the same search on the same string repeatedly.
In the list context, it will make an array of each parenthesized pattern. For example, our word search example from the last

($a, $b, $c) = /(\S*)\s*(\S*)\s*(\S*)/;

could be rewritten like this:
@array = /(\S+)\s*/g;

That will create an array with one element for each word in the string.
In fact, without parenthesis it will make the array of each match to the whole pattern. So the search can be even more abbreviated:
@array = /\S+/g;

In a scalar context, it will return 1 (true) for every successful match. For example, this loop will print each word on a line:
while(/\S+/g) { 
  print "[$&] " 
}


The special variable $& always has the last matched pattern.
In a scalar context, the /g modifier performs a progressive match. For each execution of the match, Perl starts searching from the point in the search string just past the last match. You can use this to progress through an array searching for the same string without having to remove or manually set the starting position of the search. The position of the last match can be used within a regular expression using the \G assertion. When /g fails to match, the position is reset to the start of the string.

Pattern-Matching Operators

The following list defines Perl's pattern-matching operators. Some of the operators have alternative "quoting" schemes and have a set of modifiers that can be placed directly after the operators to affect the match operation in some way.
m/pattern/gimosx

Searches a string for a pattern match. Modifiers are:
Modifier Meaning
g Match globally find all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Only compile pattern once.
s Treat string as single line.
x Use extended regular expressions.

If / is the delimiter, then the initial m is optional. With the m, you can use any pair of non-alphanumeric, non-whitespace characters as delimiters.
?pattern? This operator is just like the m/pattern/ search, except it matches only once.
qr/pattern/imosx Creates a precompiled regular expression from pattern, which can be passed around in variables and interpolated into other regular expressions. The modifiers are the same as those for m// above.
s/pattern/replacement/egimosx Searches a string for pattern, and replaces any match with the replacement text. Returns the number of substitutions made, which can be more than one with the /g modifier. Otherwise, it returns false (0). If no string is specified via the =~ or !~ operator, the $_ variable is searched and modified.

The following table contains an in depth exposition into the various types of regular expressions
Modifier Meaning
i do case-insensitive pattern matching
m treat the string as multiple lines
o only compile the pattern once
s treat the string as a single line
x use pretty-printing extensions

Modifier Meaning
Perl comments and mode modifiers

Let us look at an example of using the m// operator. Here is a simple Perl script that will take a file with a list of unorganized names and print out all the names that begin with the letter g.
Mouse over the image below to receive additional information with respect to the regular expression.

foreach-tag
  1. This is shorthand for "while(<STDIN>)." In other words, do this while there is input from the keyboard.
  2. Put whatever items are on the right of the operator into an array named @tags
  3. Match on the word boundry, i.e., match whole words.
  4. Match all words beginning with g.
  5. Ignore case and apply to all matches within the string.
Regular Expression Syntax

Perl Pattern Matching - Modifiers

Perl m operator - Exercise

Click the Exercise link below to use the m// operator to print a set of elements within a given file.
Perl m operator - Exercise