From: Bill Kelly Date: 2005-12-15T11:42:53+09:00 Subject: Re: regular expressions question From: "ako..." > > scan does not help because it can match a portion of the source string, > and what is in between the matches is skipped. so scan is just a > special case of the functionality that i was looking for. i need to > make sure the whole string has a defined structure and get parts of it > as groups. Ah, OK thanks. From your earlier post: > if i wanted to parse a list of letters separated by spaces and commas: > > 'a , b,c' =~ /^(?:(\w)\s*,\s*)*(\w)$/ > > i need to get ['a','b'] in group 1 and ['c'] in group 2. What about: 'a , b,c' =~ /^((?:\w\s*,\s*)*)(\w)$/ last_match = $2 first_matches = $1.scan(/\w/) Since we first verified the whole string conforms to the required pattern, we can then safely perform the scan on the captured group to obtain the individual matches. Or we could write the scan using look-ahead assertions, as another way to prevent the skipping of in-between parts: str = 'a , b,c' # first verify whole pattern matches, and get final match group if str =~ /^(?:\w\s*,\s*)*(\w)$/ last_match = $1 first_matches = str.scan(/(?:(\w)\s*,\s*)(?=(?:\w\s*,\s*)*\w$)/).flatten end # last_match => "c" # first_matches => ["a", "b"] HTH, Bill