简体   繁体   English

在C#中按字符分割字符串

[英]Split string by character in C#

I need to split this code by ',' in C#. 我需要在C#中用“,”分隔此代码。

Sample string: 示例字符串:

'DC0''008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-' 'DC0'008 _','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'

I can use string.split(',') but as you can see 'Comm,erc,' is split up by 我可以使用string.split(','),但正如您所看到的,'Comm,erc'由

comm 通讯
erc erc

also 'DC0''008_' should split up as 同样'DC0'008_'应该拆分为

'DC0''008_' 'DC0'008_'

not as 不像

'DC0' 'DC0'

'008_' '008_'

The expected output should be like this: 预期的输出应如下所示:

'DC0''008_' 'DC0'008_'

'23802.76' '23802.76'

'23802.76' '23802.76'

'23802.76' '23802.76'

'Comm,erc,' 'Comm,erc'

'2f17' '2f17'

'3f44c0ba-daf1-44f0-a361-' '3f44c0ba-daf1-44f0-a361-'

split can do it but regex will be more complex. split可以做到,但是regex会更复杂。

You can use Regex.Matches using this simpler regex: 您可以使用Regex.Matches使用以下更简单的正则表达式:

'[^']*'

and get all quoted strings in a collection. 并获取集合中所有引用的字符串。

Code: 码:

MatchCollection matches = Regex.Matches(input, @"'[^']*'");

To print all the matched values: 要打印所有匹配的值:

foreach (Match match in Regex.Matches(input, @"'[^']*'"))
         Console.WriteLine("Found {0}", match.Value);

To store all matched values in an ArrayList : 要将所有匹配的值存储在ArrayList

ArrayList list = new ArrayList();
foreach (Match match in Regex.Matches(input, @"'[^']*'")) {
   list.add(match.Value);
}

EDIT: As per comments below if OP wants to consume '' in the captured string then use this lookaround regex: 编辑:根据以下注释,如果OP要在捕获的字符串中使用'' ,则使用此环视正则表达式:

'.*?(?<!')'(?!')

(?<!')'(?!') means match a single quote that is not surrounded by another single quote. (?<!')'(?!')表示匹配不被另一个单引号引起来的单引号。

RegEx Demo 正则演示

You can use this Regex to get all the things inside the commas and apostrophes: 您可以使用此正则表达式获取逗号和撇号内的所有内容:

(?<=')[^,].*?(?=')

Regex101 Explanation Regex101说明

To convert it into a string array, you can use the following: 要将其转换为字符串数组,可以使用以下命令:

var matches = Regex.Matches(strInput, "(?<=')[^,].*?(?=')");
var array = matches.Cast<Match>().Select(x => x.Value).ToArray();

EDIT: If you want it to be able to capture double quotes, then the Regex that will match it in every case becomes unwieldy. 编辑:如果您希望它能够捕获双引号,则在每种情况下都将与之匹配的正则表达式变得笨拙。 At this point, It's better to just use a simpler pattern with Regex.Split : 此时,最好在Regex.Split使用更简单的模式:

var matches = Regex.Split(strInput, "^'|'$|','")
                   .Where(x => !string.IsNullOrEmpty(x))
                   .ToArray();
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "'DC0008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'";
            var matches = Regex.Matches(str, "(?<=')[^,].*?(?=')");
            var array = matches.Cast<Match>().Select(x => x.Value).ToArray();
            foreach (var item in array)
            Console.WriteLine("'" + item + "'");
        }
    }
}

it is good to modify your string then split it so that you will achieve what you want like some thing below 最好修改您的字符串然后将其分割,以便您可以像下面的东西那样实现所需的功能

string data = "'DC0008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'";

data = Process(data); //process before split i.e for the time being replace outer comma with some thing else like '@'

string[] result = data.Split('@'); // now it will work lolz not confirmed and tested

the Process() function is below Process()函数在下面

private string Process(string input)
{
   bool flag = false;
   string temp="";
   char[] data = input.ToCharArray();
   foreach(char ch in data)
   {
     if(ch == '\'' || ch == '"')
         if(flag)
           flag=false;
         else
           flag=true;

      if(ch == ',')
       {
           if(flag) //if it is inside ignore else replace with @
             temp+=ch;
           else
             temp+="@";
       }
      else
          temp+=ch;
   }
 return temp;
}

see output here http://rextester.com/COAH43918 在此处查看输出http://rextester.com/COAH43918

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

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