in reply to Complex Splitting

Quickshot:
push @array, $2 ? $2 : $3 while $str =~ /(([A-Z])|\[([A-Z]+)\])/g;


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Complex Splitting
by diotalevi (Canon) on Feb 07, 2007 at 07:58 UTC

    In perl 5.10, you don't need to do backflips to get away from the "extra" captures. (?^...|...) means the captures get renumbered so here both are $1. Only one ever actually matches at any time so there's no conflict.

    push @array, $1 while $str =~ /(?^(\w+)|\[(\w+)\])/g;

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊