From: Chris Meyers Date: 2006-10-13T09:00:26+09:00 Subject: Ruby Regexp vs Perl and C# I am having trouble with the Ruby regexp engine and don't know if it is my lack of experience with Ruby, or if it just isn't possible to do certain things with the Ruby engine. Basically I have the following code in perl and C#, simplified for the example: Perl: $line = "banana"; while( $line =~ /(an)*/g ) { print $`. "<<" . $& . ">>" . $' . "\n"; } C#: using System; using System.Text.RegularExpressions; public class MyClass { public static void Main() { string testString = "banana"; MatchCollection matches = Regex.Matches( testString, "(an)*" ); foreach( Match match in matches) { Console.WriteLine( testString.Substring( 0, match.Index) + "<<" + match.Value + ">>" + testString.Substring( match.Index + match.Length)); } } } Output from both: <<>>banana b<>a banan<<>>a banana<<>> While the C# way of getting the answer is a bit more messy, it is still possible. I am wondering if/how to do this in Ruby. From what I have seen, all the Ruby options are a single match which doesn't help me, especially with the given example where Ruby's only match would be the one before the string thus being empty. Using the string#scan in ruby also doesn't help as it gives a 2 dimensional array of the following: Ruby: irb(main):001:0> line = "banana" => "banana" irb(main):002:0> line.scan(/(an)*/) => [[nil], ["an"], [nil], [nil]] So while Ruby finds that there should be 4 matches, it returns them in a difficult to use format, with no index information, and 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 -- Posted via http://www.ruby-forum.com/.