Perl question, parsing line by line

Hello,

Im very new to PERL and as a project to work on developing my skills at PERL Im trying to parse poker hands.

Ive tried many methods however I cant get the last step.

$yourfile= 'FILENAME';#poker hands to parse
open (FILE, "$yourfile") or die $!;
@lines = <FILE>;

for (@lines)
{
if (/^Dealt.*]$/)
{
$blah = $_;
push (@cards,$blah);
}
}
print "@cards";

This script will produce the following output...

Dealt to me [4h 5s]
Dealt to me [Kh 2s]
Dealt to me [3c 6h]
Dealt to me [9s 8d]

I want to parse out the hands within the [ ], how do i do this?

if (/^Dealt .*\[([^][]+)\]$/) {
  push @cards, $1;
}

Parentheses in a regular expression "capture" whatever matches the subexpression inside the parentheses, so you can refer to the first (counting opening parentheses from the beginning of the string) as $1, the second parenthesized subexpression match as $2, etc.

The regex looks a bit complex because the delimiters [ and ] are metacharacters, so they need to be backslashed, and there are a few uses of the actual metacharacters in that expression, too. Suppose the delimiters were < and > instead (these have no special meaning in regular expressions), the expression would be /^Dealt.*<([^><]+)>$/ which is at least a little bit easier to read. In so many words, inside the delimiters, use capturing parentheses, capturing as many as possible of any character except the delimiters (or newline, implicitly).