"Clifford Heath" <cjh_nospam / managesoft.com> schrieb im Newsbeitrag
news:1052444464.885588 / excalibur.osa.com.au...
> While trying to build an RE to parse a shell-style regexp into an
> array of non-wild, wild, non-wild, wild, etc I found (again) that
> the grouping operator (), when followed by *, returns only the last
> match into the MatchData:
>
> str = 'foo*bar?baz'
> regex = Regexp.new('([*?]|(?:[^*?]+))*', Regexp::EXTENDED);
>
> matches = regex.match(str)
> p matches[1..(matches.length-1)]
>
> yields:
>
> ["baz"]

That's std behavior on all platforms.  A possible reason is that for
substitutions like with gsub you would need an additional argument that
tells which element of the array.  If you want to have the complete thing
just place another set of bracktes around.

To achieve what you want, this is better:

str = 'foo*bar?baz'
matches = str.scan /[*?]|(?:[^*?]+)/

which yields exactly what you were looking for:

["foo", "*", "bar", "?", "baz"]

> Annoying. I wanted ["foo", "*", "bar", "?", "baz"].
> How to do this most simply?
>
> Also, why is this invalid:
>
> re = Regexp.new('[a-z\\]')

Why not simply do

re = /[a-z\\]/

    robert