Home > Articles > Web Development > Perl

Regular Expressions in Perl—Pattern Matching

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss

This chapter is from the book

If you are familiar with UNIX utilities, such as vi, sed, grep, and awk, you have met face-to-face with the infamous regular expressions and metacharacters used in delimiting search patterns. Well, with Perl, they're back! Learn what they are and how to use them.

8.1 What Is a Regular Expression?

If you are familiar with UNIX utilities, such as vi, sed, grep, and awk, you have met face-to-face with the infamous regular expressions and metacharacters used in delimiting search patterns. Well, with Perl, they're back!

What is a regular expression, anyway? A regular expression is really just a sequence, or pattern, of characters that is matched against a string of text when performing searches and replacements. A simple regular expression consists of a character or set of characters that matches itself. The regular expression is normally delimited by forward slashes.1 The special scalar $_ is the default search space where Perl does its pattern matching. $_ is like a shadow. Sometimes you see it; sometimes you don't. Don't worry; all this will become clear as you read through this chapter.

Example 8.1.

1   /abc/
2   ?abc?

Explanation

  1. The pattern abc is enclosed in forward slashes. If searching for this pattern, for example, in a string or text file, any string that contained the pattern abc would be matched.
  2. The pattern abc is enclosed in question marks. If searching for this pattern, only the first occurrence of the string is matched. (See the reset function in Appendix A.)
  • Share ThisShare This
  • Save To Your Account
Perl by Example, 4th Edition

This chapter is from the book

Perl by Example, 4th Edition

Learn MoreAdd To Cart

Discussions

comments powered by Disqus

Related Resources

#TuesdayTrivia: Spotlight on WP7 (Win a copy of Sams Teach Yourself Windows Phone 7 Application Development)
By on May 2, 2012Comments
These days, what CAN'T a smartphone do? Microsoft is putting their own spin on things to help you experience "life in motion" when using your device. Instead of containing static application icons, the re-imagined Start screen features live Tiles showing real-time content updates.

March Trivia #1: Let there be light! (Win Microsoft Visual Studio LightSwitch Unleashed)
By on March 13, 2012Comments
Want a simplified self-service tool to help you build business applications for the desktop and beyond? Microsoft programmers… meet Visual Studio LightSwitch.

February Trivia #2: There's an App for That (Win Sams Teach Yourself iOS 5 Application Development in 24 Hours)
By on February 28, 2012Comments
In less than a decade, the iOS platform has changed the way we think about mobile communication.

See All Related Blogs