Bart said:
Joe Gottman wrote:
The only solution that I think works reasonably well, is a two step
approach: first match the whole list, and second split up the match into
its parts. For example, like this (though there are other approches, for
example using split):
if(/^ \s* keyword \s* : ((?: \s* \w+ \b)+)/x) {
@parts = $1 =~ /\w+/g;
}
It is worth mentioning that rather than capturing and reprocessing $1
you can take advantage of the behaviour of //g in a scalar context.
if(/^ \s* keyword \s* :/gx) {
@parts = /\G \s* (\w+)/g;
}
Note - although I say this technique is worthy mention I probably
wouldn't use it here because although it's equivalent to Bart's solution
I would actually prefer to see an end-of-line anchor in Bart's solution.
if(/^ \s* keyword \s* : ([\s\w]*)$/x) {
@parts = $1 =~ /\w+/g;
}
Yes, that is indeed making perl do the same match twice.
Of course. But as I show above the first match can actually be somewhat
simpler.
If you are feeling particularly obscure you can combine the two
techniques by using lookahead to set pos() to the middle of a pattern match.
if(/^ \s* keyword \s* : (?=[\s\w]*$)/gx) {
@parts = /\w+/g;
}
This saves the expense of performing the string copy at the expense of
being rather harder to comprehend.