View Problem
Loop through a string matching a regex and performing an action for each match
Create a list
Submit a new solution for csharp
There are 21 other solutions in additional languages (clojure, cpp, erlang, fantom ...)
[fish1,cow3,boat4] when matching "(fish):1 sausage (cow):3 tree (boat):4" with regex /\((\w+)\):(\d+)/
csharp .net 2.0 but compiled with c# 3.0
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static class extensions {
public static IList<string> Map(this string me, string pattern, Func<Match, string> action){
IList<string> matches = new List<string>();
foreach (Match match in Regex.Matches(me,pattern)){
matches.Add(action(match));
}
return matches;
}
}
class Test
{
static void Main()
{
IList<string> list = "(fish):1 sausage (cow):3 tree (boat):4".Map(@"\((\w+)\):(\d+)", (m) => {return m.Groups[1].Value + m.Groups[2].Value;});
}
}
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static class extensions {
public static IList<string> Map(this string me, string pattern, Func<Match, string> action){
IList<string> matches = new List<string>();
foreach (Match match in Regex.Matches(me,pattern)){
matches.Add(action(match));
}
return matches;
}
}
class Test
{
static void Main()
{
IList<string> list = "(fish):1 sausage (cow):3 tree (boat):4".Map(@"\((\w+)\):(\d+)", (m) => {return m.Groups[1].Value + m.Groups[2].Value;});
}
}
Submit a new solution for csharp
There are 21 other solutions in additional languages (clojure, cpp, erlang, fantom ...)


