in reply to Matching elements in a array

duckyd's solution checks that all lines have at least one valid tag in them. I think you were looking for a check that each tag, when present, is a valid tag, skipping over any lines that don't have tags.

Also, if there's more than one tag on a line, you probably want to check all of them. You can get all the tags to check easily with the "g" option to the m// match operator, if you use it in an array context. Note that the match has to be non-greedy, using "+?" instead of "+", otherwise it will lump tags toghether.

Try this, which catches the second tag in the last line. (tested)

my $allowed = qr{kt$|bold$|ital$|ch}; while (defined (my $line = <DATA>)) { die "Element $1 invalid,line #$.\n" if (grep { $_ !~ /^$allowed/ } $line =~ /<([^>].+?)>/g); } __DATA__ <kt> <bold> <ital> <ch1> <ch_named> Line of content this line has an unspecified <ch> tag <ch><Bold>