Basic Regular Expressions

Suppose that you have a configuration file in which any line beginning with # is a comment. You want to read the file and throw away comments. How do you tell which lines are comments?

The answer is to use Perl’s regular expression operators. A regular expression is a powerful pattern matching tool. It enables you to match, and replace if needed, virtually any text you can imagine. Regular expressions are enclosed in slashes (//). The slashes are similar to the double quotes ("") used for strings. This chapter starts with a simple regular expression and builds up to more complex comment matching ones. The first regular expression is

/#/

This matches any string with a hash character (#) anywhere within the string. For ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.