#!/usr/bin/perl -wl use strict; #use re 'eval'; $\= "\n"; my $qred = qr{ MT: (\")? ([^\"]+) (?(1) \"\s | \s) }x; for( ' MT:yes ' , 'MT:NO" ', 'MT:"yes" ', 'MT:"NO ' ) { print; print $2 if /$qred/; print '-' x 33; } eval q{ require YAPE::Regex::Explain; print YAPE::Regex::Explain->new($qred)->explain; }; __END__ MT:yes yes --------------------------------- MT:NO" --------------------------------- MT:"yes" yes --------------------------------- MT:"NO --------------------------------- The regular expression: (?x-ims: MT: (")? ([^"]+) (?(1) "\s | \s) ) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?x-ims: group, but do not capture (disregarding whitespace and comments) (case-sensitive) (with ^ and $ matching normally) (with . not matching \n): ---------------------------------------------------------------------- MT: 'MT:' ---------------------------------------------------------------------- ( group and capture to \1 (optional (matching the most amount possible)): ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- )? end of \1 (NOTE: because you're using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \1) ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- [^"]+ any character except: '"' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- (?(1) if back-reference \1 matched, then: ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- | else: ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- ) end of conditional on \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------