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!
Wednesday, August 15th, 2001
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.
Read More
- Helpful Tools for Software Developers
- The Github Hall of Fame
- Book'em, Github.
- This Week on Github: Stupid Ruby Tricks
- A Veritable Scatter Shot!
Rackspace
|