in reply to how do I detect an arbitrary number of $1, $2...

Use /g and capture the results of your match in an array.

$_ = 'BODY, A, DIV, H1, H2, H3, H4, TABLE, TR, TH, TD, INPUT, TEXTAREA +'; my @tags = /(\w+)/g; print map { "<$_>\n" } @tags;