"Perl for Perl Newbies" - Part 2Regular Expressions

7. Regular Expressions

Regular expressions are one of the most powerful features of Perl, but are also one of those that beginners find most difficult to understand. Don't let that fact discourage you: they are definitely worth your time.

Basically, a regular expression tells the interpreter to search a string for a pattern. The pattern can be a simple one like the word "hello" or a complex one, such as the word "hello" followed by any number of instances of the word "one", followed by the word "triple". The latter will be written as hello(?:one)*triple inside a regular expression.

The perl operators allow you to do three things with a regular expression:

  1. Check if it exists in the string at all.
  2. Retrieve various captures out of it.
  3. Retrieve all of its occurrences.
  4. Substitute the first occurrence or all of its occurrences with some other string or (perl) expression.

Some perl functions such as split and grep (which will be covered later) can utilise regular expressions to do other tasks.

Written by Shlomi Fish