Parse string with whitespace and quotation mark (with quotation mark retained)

前端 未结 3 1215
心在旅途
心在旅途 2020-12-11 04:42

If I have a string like this

create myclass \"56, \'for the better or worse\', 54.781\"

How can I parse it such that the resul

3条回答
  •  没有蜡笔的小新
    2020-12-11 05:48

    You can split by this

    \s(?=(?:[^"]*"[^"]*")*[^"]*$)
    

    See demo.

    https://regex101.com/r/fM9lY3/60

    string strRegex = @"\s(?=(?:[^""]*""[^""]*"")*[^""]*$)";
    Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
    string strTargetString = @"create myclass ""56, 'for the better or worse', 54.781""";
    
    return myRegex.Split(strTargetString);
    

提交回复
热议问题