简体   繁体   English

如果逗号不在两个双引号之间,请用逗号分隔

[英]Split by comma if that comma is not located between two double quotes

I am looking to split such string by comma : 我想用逗号分割这样的字符串:

 field1:"value1", field2:"value2", field3:"value3,value4"

into a string[] that would look like: 到一个看起来像的string[]

0     field1:"value1"
1     field2:"value2"
2     field3:"value3,value4"

I am trying to do that with Regex.Split but can't seem to work out the regular expression. 我试图用Regex.Split做到这一点,但似乎Regex.Split正则表达式。

It'll be much easier to do this with Matches than with Split , for example 例如,使用Matches比使用Split更容易做到这一点

string[] asYouWanted = Regex.Matches(input, @"[A-Za-z0-9]+:"".*?""")
    .Cast<Match>()
    .Select(m => m.Value)
    .ToArray();

although if there is any chance of your values (or fields!) containing escaped quotes (or anything similarly tricky), then you might be better off with a proper CSV parser. 虽然如果你的值(或字段!)有任何机会包含转义引号(或任何类似的棘手),那么你可能最好使用适当的CSV解析器。


If you do have escaped quotes in your values, I think the following regex the work - give it a test: 如果已经在你的价值观转义引号,我认为下面的正则表达式工作-给它一个测试:

@"field3:""value3\\"",value4""", @"[A-Za-z0-9]+:"".*?(?<=(?<!\\)(\\\\)*)"""

The added (?<=(?<!\\\\)(\\\\\\\\)*) is supposed to make sure that the " it stops matching on is preceeded by only an even number of slashes, as an odd number of slashes means it is escaped. 添加的(?<=(?<!\\\\)(\\\\\\\\)*)应该确保"它停止匹配在前面只有偶数个斜线,因为奇数个斜线意味着它被逃脱了。

Untested but this should be Ok: 未经测试但这应该是好的:

string[] parts = string.Split(new string[] { ",\"" }, StringSplitOptions.None);

remember to add the " back on the end if you need it. 记得在需要时添加“返回结尾”。

string[] arr = str.Split(new string[] {"\","}}, StringSplitOptions.None).Select(str => str + "\"").ToArray();

\\,分割\\,如webnoob所提到的那样后缀为尾随"使用select,然后强制转换为数组。

try this 尝试这个

// (\w.+?):"(\w.+?)"        
//         
// Match the regular expression below and capture its match into backreference number 1 «(\w.+?)»        
//    Match a single character that is a “word character” (letters, digits, and underscores) «\w»        
//    Match any single character that is not a line break character «.+?»        
//       Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»        
// Match the characters “:"” literally «:"»        
// Match the regular expression below and capture its match into backreference number 2 «(\w.+?)»        
//    Match a single character that is a “word character” (letters, digits, and underscores) «\w»        
//    Match any single character that is not a line break character «.+?»        
//       Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»        
// Match the character “"” literally «"»        


try {        
    Regex regObj = new Regex(@"(\w.+?):""(\w.+?)""");        
    Match matchResults = regObj.Match(sourceString);        
    string[] arr = new string[match.Captures.Count];        
    int i = 0;        
    while (matchResults.Success) {        
        arr[i] = matchResults.Value;        
        matchResults = matchResults.NextMatch();        
        i++;        
    }         
} catch (ArgumentException ex) {        
    // Syntax error in the regular expression        
}

The easiest inbuilt way is here . 最简单的内置方式就在这里 I checed it . 我把它弄了。 It is working fine. 它工作正常。 It splits "Hai,\\"Hello,World\\"" into {"Hai","Hello,World"} 它将"Hai,\\"Hello,World\\""分成{"Hai","Hello,World"}

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

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