A valid ref spec is a string that starts with the literal 'element ', and is followed by * Either a star '*' OR by m/-dir(ectory)? \S+/ OR by m/-file \S+/ * then by a label A label is ... #### my $re = qr{ element \s ( \* | -dir(ectory)? \S+ | -file \S+ ) # Label: \w+ }x;