my $r = q( <(\D+)> # Opening tag <....> ([^/]*?) # stuff in the middle which does not have the closing tab character '/' (<\/\1>) # closing tag of same type as opening tag . ); while ($text =~ m/$r/gix){ ... processing ... }