Regular Expressions

Who am I?


Table of Contents


The Regular Expression Grammar
Rules For Matching
Storing Regexes
Using Regexes
Regex Memory
Interpolating into Regexes
Evaluated Replacements
Extended Regexes
Greed
Tips

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. — Jamie Zawinski
This quote illustrates two things about Regular Expressions: their versatility and their complexity.

The Regular Expression Grammar

The Basics

Quantifiers

Alternation

Character Classes

Anchors

Escaped Characters

Case Sensitivity

Rules For Matching

Storing Regexes

Using Regexes

There are three primary operations in Perl that use regular expressions: matching, substituting, and splitting.

Matching Using Regexes

Substitution Using Regexes

Split Using Regexes

Regex Memory

Interpolating into Regexes

Building Complex Regexes

Constructing Regexes Programmatically

Evaluated Replacements

Extended Regexes

Greed

Transliteration

Summary of Pattern Modifiers

Several regex modifiers have been mentioned so far. Here is a list of the important ones. For more information, see the chapter "Pattern Matching" in Programming Perl.

ModifierEffectm//s///qr//Bad Mnemonic?
/imatch case-Insensitivelyyesyesyesno
/sinclude \n in the dot class
(force string to be handled as a Single line)
yesyesyesyes
/m^ and $ can match before or after an ebmedded newline
(force string to be handled as Multiple lines)
yesyesyesyes
/xeXtended regex (can include comments and whitespace)yesyesyesno
/oonly compile pattern Onceyesyesno
/gmatch/substitute Globallyyesyesno
/cContinue after a failed global matchwith /gno
/eEvaluate replacement as expressionyesno

Tips

When not to use Regular Expressions

Good Grief, Make It Stop!

Answers to discussion questions: