从字符串中提取多个空格

Bac*_*ave 4 c# regex

我希望获得大于1个空格的空白区域.

以下内容为我提供了每个字母之间的空字符,以及白色空格.不过,我只是想提取两者之间的空格串cd,以及之间的3个空格串fg.

string b = "ab c  def   gh";
List<string> c = Regex.Split(b, @"[^\s]").ToList();
Run Code Online (Sandbox Code Playgroud)

更新:以下工作,但我正在寻找一种更优雅的方式来实现这一目标:

c.RemoveAll(x => x == "" || x == " ");
Run Code Online (Sandbox Code Playgroud)

期望的结果将是List<string>包含" "" "

Dmi*_*nko 5

如果您希望List<String>作为结果,您可以执行此Linq查询

string b = "ab c  def   gh";

List<String> c = Regex
  .Matches(b, @"\s{2,}")
  .OfType<Match>()
  .Select(match => match.Value)
  .ToList();
Run Code Online (Sandbox Code Playgroud)