【问题标题】:Split string at space not inside quotes in C# [duplicate]在 C# 中不在引号内的空格处拆分字符串 [重复]
【发布时间】:2011-04-05 03:05:52
【问题描述】:

可能重复:
Regular Expression to split on spaces unless in quotes

我需要在所有空格处拆分指定的字符串以获取参数,但是,我需要引号内的所有文本保持“未拆分”。例如:

ab "c d"

这需要分成 2 个字符串:1. ab, 2. "c d"。我尝试使用正则表达式或编写自己的方法来解析它,但没有运气。

【问题讨论】:

    标签: c# regex string split space


    【解决方案1】:

    绝对是上面提到的 Bala R 的复制品。

    Regex regex = new Regex(@"\w+|""[\w\s]*""");
    var str = "ab \"c d\"";
    
    var x = regex.Matches(str).Cast<Match>().ToList();
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多