Contents Up Previous Next

7 Regular Expressions

Regular expressions are one of the most powerful features of perl, but are also one of the 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 as many instances of the word "one" as possible, 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 all of its occurences.
  3. Substitue the first occurence or all of its occurences with some other string or (perl) expression.

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


Contents Up Previous Next