Mon 15 May 2006
Or Regex:
Consider the following regex:
/A(dam|nne|ndrew)/
What does it do? In many computer languages, a vertical pipe ( | ) means OR, and Perl is no exception. The regex matches the names “Adam”, “Anne” OR “Andrew” - any one will do. There is however a cost - because we used parenthesis, $1 is created, and filled with the value Adam, Anne or whatever. This is wasteful, so in the interests of efficiency, we have the following alternative convention (which doesn’t create $1):
/A(?:dam|nne|ndrew)/
There is yet another convention (Perl is stuffed with them, isn’t it) that allows us to pull text out of a string without using parenthesis! If we use the “variables” $`, $& and $’ just after some regex, then they will respectively contain (1) the text before the match, (2) the matched text, and (3) the text after the match. Avoid them - there’s a time penalty if you use them at all. The terrible thing is that if you use any of these variables anywhere in your program, Perl will provide them for all regex! (An aside: $+ returns the most recent parenthesis variable match).