next up previous contents
Next: Constructing Regular Expressions Up: Regular Expressions Previous: Introduction to Regular Expressions   Contents

Operators to Work with Regular Expressions

While we'll look at these operators in more detail later, it's worth understanding them before setting out to fully understand regular expressions. The basic match operator for regular expressions has the form:
     m/regexp/
where regexp represents a regular expression constructed as described in the following sections. You can use any non-alphanumeric character you like in place of the slash (/), and, in fact, the ``m'' is only required when you use a character other than the slash. Most perl programmers omit the ``m'' when they are using the slash as a delimiter.

Since regular expressions are used so widely when reading files, and since files in perl are usually read line by line into the variable $_, by default the match operator attempts to match its regular expression to that variable, returning a true value if the regular expression is found in $_, and a false value if it is not. It is also possible to test regular expressions against any scalar character variable in perl by using the special regular expression operator, =~. Thus to see if the string ``cat'' was anywhere in the default variable $_, you would test the expression /cat/, while to do the same for the variable $text, you would test the expression $text =~ /cat/. For added convenience, !~ can be used as a non-matching regular expression operator, that is, an expression like $text !~ /cat/ will be true if ``cat'' was not found in the variable $text.

For the closely related task of substitution, that is, replacing a regular expression in text with some other text string, the ``s'' operator can be used. It's general form is

     s/regexp/string/
While the ``s'' must always be present to perform a substitution, the slashes can be replaced with any non-alphanumeric character of your choice. Like the match operator, the substitute operator works by default on $_, and can be made to operate on any other scalar string value by using the =~ operator.

Remember that the regular expression operators always operate in a scalar context; testing an array for a regular expression will rarely give the desired result. If you wish to extract those elements of an array which contain a regular expression, you must either use a loop or the grep function. This function actually has lots of other capabilities, but in its simplest form, you provide a match operator containing a regular expression as the first argument, and the array to be searched as the second argument. The function will return an array containing all the elements of the original array which matched the regular expression. Suppose we have an array called animals, and we wish to check to see if any of the elements of the array are matched by the pattern ``ick''

  @animals = ('duck','goat','chicken','cow','dog','cat');
  if(grep(/ick/,@animals)){
     print "Match found\n";
  }
Since ``chicken'' contains the pattern in question, the grep function will return an array containing that element, and, when evaluated in a scalar context by the if statement, it will evaluate as 1 (since there is one element in the array), and the message would be printed. More details of the grep function can be found in Section [*].

With these preliminaries out of the way, let's look at how to construct regular expressions.


next up previous contents
Next: Constructing Regular Expressions Up: Regular Expressions Previous: Introduction to Regular Expressions   Contents
Phil Spector 2002-10-18