Skip to content

Instantly share code, notes, and snippets.

@masaeedu
Created May 17, 2015 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaeedu/d89eb47ab7dcb3e269b8 to your computer and use it in GitHub Desktop.
Save masaeedu/d89eb47ab7dcb3e269b8 to your computer and use it in GitHub Desktop.
Succintly doing string operations using LINQ and regex
var reg = new Regex(@"(\S+) \({([ \d]+)}\)");
var myStr = @"NULL ({ 8 9 36 37 }) John ({ 1 }) Loizou ({ 2 3 }) delves ({ 4 }) into ({ 5 })";
var lines = reg.Matches(myStr).Cast<Match>().Select(m =>
{
var prefix = m.Groups[1];
var nums = m.Groups[2].Value.Split().Where(s => !string.IsNullOrWhiteSpace(s));
return string.Format("{0} {1}_", prefix, string.Join(",", nums));
});
Console.WriteLine(string.Join(Environment.NewLine, lines));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment