next up previous
Next: Substitution Up: Regular Expressions Previous: Matching


Capturing

In perl, you can slurp up any text matched in a regular expression by putting parentheses around it. The parenthesized parts of a successful regular expression match will be put in the variables $1, $2, $3, ... in order according to where the parenthesis are in the regular expression. For example, the following regular expression will put the four parts of the ip address in $1, $2, $3, and $4.

$log_line =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)\s/;

altrernatively, if we wanted to grab the whole ip address in one operation, we could put the parentheses around the whole ip address expression.

$log_line = /^(\d+\.\d+\.\d+\.\d+)\s/;



saville stephen a 2003-01-05