Skip to content

Instantly share code, notes, and snippets.

@brandonc
Created June 22, 2011 20:46
Show Gist options
  • Save brandonc/1041133 to your computer and use it in GitHub Desktop.
Save brandonc/1041133 to your computer and use it in GitHub Desktop.
Reduce an array using regex in C# and ruby
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Program
{
class XFiles
{
static int Main(string[] argv)
{
string[] names = { "Mark", "Richard", "Fox", "Scully" };
var r = new Regex("(Fox|Scully)");
var xfiles = names.Where(name => r.IsMatch(name)).ToArray();
Console.WriteLine("Everyone: {0}", String.Join(", ", names));
Console.WriteLine(" X-Files: {0}", String.Join(", ", xfiles));
return 0;
}
}
}
names = ["Mark", "Richard", "Fox", "Scully"]
xfiles = names.select do |name|
name =~ /(Fox|Scully)/
end
puts "Everyone: #{names.join(", ")}"
puts " X-Files: #{xfiles.join(", ")}"
@brandonc
Copy link
Author

193 vs 569 chars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment