O'Reilly logo

Learning Perl, 7th Edition by Tom Phoenix, brian d foy, Randal L. Schwartz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Matching with Regular Expressions

In Chapter 7, you visited the world of Regular Expressions. Now you’ll see how that world fits into the world of Perl.

Matches with m//

You put patterns in pairs of forward slashes, like /fred/, but this is actually a shortcut for the m//, the pattern match operator. As you saw with the qw// operator, you may choose any pair of delimiters to quote the contents. So, you could write that same expression as m(fred), m<fred>, m{fred}, or m[fred] using those paired delimiters, or as m,fred,, m!fred!, m^fred^, or many other ways using nonpaired delimiters.

Note

Nonpaired delimiters are the ones that don’t have a different “left” and “right” variety; the same punctuation mark is used for both ends.

If you choose the forward slash as the delimiter, you may omit the initial m. Since Perl folks love to avoid typing extra characters, you’ll see most pattern matches written using slashes, as in /fred/.

Of course, you should wisely choose a delimiter that doesn’t appear in your pattern. If you wanted to make a pattern to match the beginning of an ordinary web URL, you might start to write /http:\/\// to match the initial "http://". But that would be easier to read, write, maintain, and debug if you used a better choice of delimiter: m%http://%. It’s common to use curly braces as the delimiter. If you use a programmer’s text editor, it probably has the ability to jump from an opening curly brace to the corresponding closing one, which can be handy in maintaining ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required