use warnings; use strict; use YAPE::Regex::Explain; my $re = '(.+?)<\/a>(.+?)<\/b><\/container>(<\?query\?>)??'; print YAPE::Regex::Explain->new($re)->explain; #### The regular expression: (?-imsx:(.+?)<\/a>(.+?)<\/b><\/container>(<\?query\?>)??) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- '' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .+? any character except \n (1 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- < '<' ---------------------------------------------------------------------- \/ '/' ---------------------------------------------------------------------- a> 'a>' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- .+? any character except \n (1 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- < '<' ---------------------------------------------------------------------- \/ '/' ---------------------------------------------------------------------- b>< 'b><' ---------------------------------------------------------------------- \/ '/' ---------------------------------------------------------------------- container> 'container>' ---------------------------------------------------------------------- ( group and capture to \3 (optional (matching the least amount possible)): ---------------------------------------------------------------------- < '<' ---------------------------------------------------------------------- \? '?' ---------------------------------------------------------------------- query 'query' ---------------------------------------------------------------------- \? '?' ---------------------------------------------------------------------- > '>' ---------------------------------------------------------------------- )?? end of \3 (NOTE: because you're using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \3) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------