wandersman has asked for the wisdom of the Perl Monks concerning the following question:

I have simplified this example. The way I'm describing it, it would be easier to use a list. with a line for example:
BODY, A, DIV, H1, H2, H3, H4, TABLE, TR, TH, TD, INPUT, TEXTAREA
I want "body" to go in $1, "a" in $2 etc if I do this
/(\w+)\,\s?(\w+)\,\s?(\w+)\,\s?(\w+)\,\s?/
it works but I it is bad looking code and breaks easily when giving different nubmers of arguments. if I do:
/(\w+)\,\s?/

Replies are listed 'Best First'.
Re: how do I detect an arbitrary number of $1, $2...
by davorg (Chancellor) on Jul 30, 2001 at 19:33 UTC

    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;