I was reading a regex group matching question and I see that there are two ways to reference capture groups from a regex expression, namely,
- Match string method e.g.
string.match(/(^.*)(:)(.*)/i).captures
- Perl-esque capture group variables such as $1, $2, etc obtained from
if match =~ /(^.*)(:)(.*)/i
- Update: As mentioned by 0xCAFEBABE there is a third option too - the last_match method
Which is better? With 1), for safety, you would have to use an if statement to guard against nils so why not just extract the information then? Instead of a second step calling the string captures method. So option 2) looks more convenient to me.