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.

Click each modifier for more information


Modifier Meaning
g match globally; that is, find all occurrences
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

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