Created
May 17, 2015 05:41
-
-
Save masaeedu/d89eb47ab7dcb3e269b8 to your computer and use it in GitHub Desktop.
Succintly doing string operations using LINQ and regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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