my $winner = $1 if $R =~ /(\[[^#]*(?:\]|#[^#]*\]))/; #### my $winner = $1 if $R =~ /(\[[^#]*(?:#[^#]*)?\])/; #### /( # Start capturing. \[ # A literal left bracket. [^#]* # Anthing that is not an octothorp (?: # Group without capturing \# # A literal octothorp [^#]* # Anything that is not an octothorp )? # End group. Group is optional. \] # literal right bracket. )/x