> Chris Meyers wrote:
> ...
> ... and [Ruby] matches only "an" rather than "anan" as I want.
> 
> So basically is there a way to do this in Ruby?  Which can also be asked 
> as, how do you do multiple matches in Ruby with full match information?
> 
> Thanks,
> Chris


It's a bit tricky but still doable!

line = "banana"

If your goal is to match just (an)* in "banana" use:

line.scan(/((an)*)/) { |str|
#puts $1.inspect
puts $1 if $1 != ""
}


To match surrounding characters as well use:

line.scan(/([^a]|a(?=[^n]|$)|(an)*)/) { |str|
puts $1.inspect unless $1.empty?
}


Additional characters can be inserted this way:

line = "bananaxyzantuia"

str = ""

line.scan(/([^a]|a(?=[^n]|$)|(an)*)/) {

match = $1

puts match  if match =~ /^[^a]$/
puts match  if match =~ /^a$/
puts match  if match =~ /^(an)+$/

if match =~ /^[^a]$/ then str << match
elsif match =~ /^a$/ then str << match
elsif match =~ /^(an)+$/ then str << "<<" << match << ">>"
end

}

puts str   #=>   b<<anan>>axyz<<an>>tuia


Instead of if-elsif-end you can also use case-when-end of course (cf. 
http://www.bigbold.com/snippets/posts/show/1313 ).


Cheers,
verno


-- 
Posted via http://www.ruby-forum.com/.