x
Loading
 Loading
Hello, Guest | Login | Register

Regular Expressions in Perl

One of the things that distinguishes Perl as a powerful and practical tool in the Linux toolbox is its ability to wrangle text in interesting ways that makes it seem effortless. A majority of that ability can be attributed to Perl’s very powerful regular expressions. Regular expressions are nothing new. I was using them with Unix tools in 1977, and I suspect they go back even further than that. But Perl continues to push the envelope of how regular expressions work; so much so that the GNU project includes a “perl-compatible-regular-expressions” library (PCRE) so that other tools can catch up to Perl!

One of the things that distinguishes Perl as a powerful and practical tool in the Linux toolbox is its ability to wrangle text in interesting ways that makes it seem effortless. A majority of that ability can be attributed to Perl’s very powerful regular expressions. Regular expressions are nothing new. I was using them with Unix tools in 1977, and I suspect they go back even further than that. But Perl continues to push the envelope of how regular expressions work; so much so that the GNU project includes a “perl-compatible-regular-expressions” library (PCRE) so that other tools can catch up to Perl!

But before we get to the advanced stuff, let’s quickly review the basics. A regular expression defines a template to match against a string. We say the string either matches or doesn’t match the given regular expression, based on whether the string has properties that the expression demands.

For example, the regular expression /a/ demands that there be the letter a somewhere in the string. If there’s a choice, and it matters, it matches the leftmost “a” (this is called the “leftmost rule”). (We typically write the regular expression enclosed in forward slashes, because that’s the most common use in Perl, though there are others.)

Of the “atoms” in a regular expression (from which the regular expression is built), the most common are the ordinary characters (such as the letter a above), including any special characters preceded by a backslash. Everything that works in a…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace