eval { $my_text =~$regex # where $regex is m/ab|(cd)/ for example print $&; print $1, $2 # if defined }