C#:153c 144c 142c 111c 115c 118c 114c 113c
(通过“ C#语句”模式下的LINQPad,不包括输入字符串)
版本1:142c
var s = "This is a text and a number: 31."; // <- line not included in count
s.Split(s.Where(c=>!Char.IsLetterOrDigit(c)).ToArray(),(StringSplitOptions)1).GroupBy(x=>x,(k,e)=>new{s,c=e.Count()}).OrderBy(x=>-x.c).Dump();
取消高尔夫:
var s = "This is a text and a number: 31.";
s.Split( // split string on multiple separators
s.Where(c => !Char.IsLetterOrDigit(c)) // get list of non-alphanumeric characters in string
.ToArray(), // (would love to get rid of this but needed to match the correct Split signature)
(StringSplitOptions)1 // integer equivalent of StringSplitOptions.RemoveEmptyEntries
).GroupBy(x => x, (k, e) => new{ s = k, c = e.Count() }) // count by word
.OrderBy(x => -x.c) // order ascending by negative count (i.e. OrderByDescending)
.Dump(); // output to LINQPad results panel
结果:

版本2:114c
([\w]
包括_
,这是不正确的!;[A-z]
包括[ \ ] ^ _ `
;确定于[^_\W]+
)
var s = "This is a text and a number: 31."; // <- line not included in count
Regex.Matches(s, @"[^_\W]+").Cast<Match>().GroupBy(m=>m.Value,(m,e)=>new{m,c=e.Count()}).OrderBy(g=>-g.c).Dump();
取消高尔夫:
Regex.Matches(s, @"[^_\W]+") // get all matches for one-or-more alphanumeric characters
.Cast<Match>() // why weren't .NET 1 collections retrofitted with IEnumerable<T>??
.GroupBy(m => m.Value, (m,e) => new{ m, c = e.Count() }) // count by word
.OrderBy(g => -g.c) // order ascending by negative count (i.e. OrderByDescending)
.Dump(); // output to LINQPad results panel
结果:(作为版本1)
This
与this
和相同tHIs
)?