简体   繁体   English

正则表达式在带引号的多个分隔符上拆分字符串

[英]Regex split string on multiple separators with quotes

I have the following string (the double quotes are part of the string): 我有以下字符串(双引号是字符串的一部分):

"abc def ghi" "%1" "%2"

So the string starts with a double quote, each segment is separated by " " and the string ends with a double quote again. 因此,字符串以双引号开头,每个段都由" "分隔,并且字符串再次以双引号结尾。 I would like to split this into: 我想将其分为:

abc def ghi
%1
%2

I tried the following: "(^\\")|(\\" \\")|(\\"$)" but that doesn't give me the desired result. 我尝试了以下操作: "(^\\")|(\\" \\")|(\\"$)"但这没有给我想要的结果。

Don't complicate it. 不要使它复杂化。 Just use a string split: 只需使用字符串拆分:

        string test = "\"abc def ghi\" \"%1\" \"%2\"";
        var splits = test.Split(new string[]{"\" \"","\""},StringSplitOptions.RemoveEmptyEntries);
        foreach (var split in splits)
        {
            Console.WriteLine(split);
        }

( removes the superfluous / empty entries as well) (也删除多余的/空的条目)

Regex regex = new Regex("\"(.*?)\"");

Results: 结果:

http://rubular.com/r/lXbDIpkRRQ http://rubular.com/r/lXbDIpkRRQ

string s = "\"abc def ghi\" \"%1\" \"%2\"";
string[] splittedStrings = s.Split('"');
string a = splittedStrings[1];
string b = splittedStrings[3];
string c = splittedStrings[5];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM