在逗号、空格或分号分隔字符串上使用Regex.Split()

时间:2022-02-02 21:45:08

I'm trying to split a string that can either be comma, space or semi-colon delimitted. It could also contain a space or spaces after each delimitter. For example

我试着分割一个字符串,可以是逗号,空格,或者是分号。它还可以在每个分隔符之后包含一个空间或空格。例如

22222,11111,23232 
OR
22222, 11111, 23232 
OR
22222;     11111; 23232
OR
22222 11111 23232 

Any one of these would produce an array with three values ["22222","11111","23232"]

其中任何一个都将生成一个具有三个值的数组["22222"、"11111"、"23232"]

So far I have var values = Regex.Split("22222, 11111, 23232", @"[\\s,;]+") but this produces an array with the second and third values including the space(s) like so:

到目前为止,我有var值= Regex。Split(“22222,11111,23232”,@“[\ s,;]+”),但这会生成一个具有第二个和第三个值的数组,包括如下所示的空间:

["22222"," 11111"," 23232"]

5 个解决方案

#1


30  

You have two possibilities:

你有两个可能性:

In this case, you want to split your string by specific delimiters caracters. String.Split has been created for this special purpose. This method will be faster than Regex.Split.

在这种情况下,您希望通过特定的分隔符特性来分割字符串。字符串。为此目的而创建了Split。这个方法将比Regex.Split要快。

char[] delimiters = new [] { ',', ';', ' ' };  // List of your delimiters
var splittedArray = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

#2


8  

You are using an @ symbol for your string, so the "\" is being interpreted as a literal slash. So your character class is actually reading as a "\", an "s", a "," or a ";". Remove the extra slash and it should work as desired:

您使用@符号表示您的字符串,因此“\”被解释为一个字斜杠。所以你的角色类实际上是读成“\”、“s”、“a”、“”或“;”删除多余的斜线,它应该可以按需要工作:

var values = Regex.Split("22222, 11111, 23232", @"[\s,;]+")

#3


2  

Regex.Split("22222, 11111, 23232", @"[ ,;]+")

this worked for me

这为我工作

Also check answer below, if all you really need is split a string based on few char delimiters - string.split is probably a better solution

还要检查下面的答案,如果你真正需要的是基于几个字符分隔符-字符串分割一个字符串。拆分可能是更好的解决方案

#4


1  

To interpret "I'm trying to split a string that can either be comma, space or semi-colon delimited. It could also contain a space or spaces after each delimiter" literally, try:

要解释“我试图分割一个字符串,该字符串可以是逗号、空格或分号分隔。”它也可以在每个分隔符之后包含一个空格或空格。

@"[,;]\s*|\s+"

This has the property that consecutive delimiters (except white space) will not be treated as a single delimiter.

它具有这样的属性,即连续的分隔符(除了空格)不会被视为单个的分隔符。

But if you want all consecutive delimiters to be treated as one, you might as well do:

但是如果你想让所有连续的定界符都被当作一个,你也可以这样做:

@"[,;\s]+"

Of course, in that case, string.Split is a simpler option, as others have indicated.

当然,在这种情况下,是字符串。正如其他人所指出的,拆分是一个更简单的选择。

#5


0  

Try this Regex pattern:

试试这个正则表达式模式:

([^,;\"\}\{\s*.]\d+)

For sample text:

示例文本:

{"123","456","789"}
1011,1213,1415
16, 17, 181920
212223;        242526;27
28 29 3031 

See demo.

看到演示。

在逗号、空格或分号分隔字符串上使用Regex.Split()

#1


30  

You have two possibilities:

你有两个可能性:

In this case, you want to split your string by specific delimiters caracters. String.Split has been created for this special purpose. This method will be faster than Regex.Split.

在这种情况下,您希望通过特定的分隔符特性来分割字符串。字符串。为此目的而创建了Split。这个方法将比Regex.Split要快。

char[] delimiters = new [] { ',', ';', ' ' };  // List of your delimiters
var splittedArray = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

#2


8  

You are using an @ symbol for your string, so the "\" is being interpreted as a literal slash. So your character class is actually reading as a "\", an "s", a "," or a ";". Remove the extra slash and it should work as desired:

您使用@符号表示您的字符串,因此“\”被解释为一个字斜杠。所以你的角色类实际上是读成“\”、“s”、“a”、“”或“;”删除多余的斜线,它应该可以按需要工作:

var values = Regex.Split("22222, 11111, 23232", @"[\s,;]+")

#3


2  

Regex.Split("22222, 11111, 23232", @"[ ,;]+")

this worked for me

这为我工作

Also check answer below, if all you really need is split a string based on few char delimiters - string.split is probably a better solution

还要检查下面的答案,如果你真正需要的是基于几个字符分隔符-字符串分割一个字符串。拆分可能是更好的解决方案

#4


1  

To interpret "I'm trying to split a string that can either be comma, space or semi-colon delimited. It could also contain a space or spaces after each delimiter" literally, try:

要解释“我试图分割一个字符串,该字符串可以是逗号、空格或分号分隔。”它也可以在每个分隔符之后包含一个空格或空格。

@"[,;]\s*|\s+"

This has the property that consecutive delimiters (except white space) will not be treated as a single delimiter.

它具有这样的属性,即连续的分隔符(除了空格)不会被视为单个的分隔符。

But if you want all consecutive delimiters to be treated as one, you might as well do:

但是如果你想让所有连续的定界符都被当作一个,你也可以这样做:

@"[,;\s]+"

Of course, in that case, string.Split is a simpler option, as others have indicated.

当然,在这种情况下,是字符串。正如其他人所指出的,拆分是一个更简单的选择。

#5


0  

Try this Regex pattern:

试试这个正则表达式模式:

([^,;\"\}\{\s*.]\d+)

For sample text:

示例文本:

{"123","456","789"}
1011,1213,1415
16, 17, 181920
212223;        242526;27
28 29 3031 

See demo.

看到演示。

在逗号、空格或分号分隔字符串上使用Regex.Split()