简体   繁体   English

Regex.Split()用逗号,空格或分号分隔的字符串

[英]Regex.Split() on comma, space or semi-colon delimitted string

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 values = Regex.Split("22222, 11111, 23232", @"[\\\\s,;]+")但这会生成一个包含第二个和第三个值的数组,包括像这样的空格:

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

You have two possibilities: 你有两种可能性:

In this case, you want to split your string by specific delimiters caracters. 在这种情况下,您希望通过特定的分隔符caracters分割字符串。 String.Split has been created for this special purpose. String.Split是为此特殊目的而创建的。 This method will be faster than Regex.Split . 此方法将比Regex.Split更快。

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

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 ";" 所以你的角色类实际上是以"\\""s"","";" . Remove the extra slash and it should work as desired: 删除额外的斜杠,它应该按照需要工作:

var values = Regex.Split("22222, 11111, 23232", @"[\s,;]+")
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 另外检查下面的答案,如果您真正需要的是基于少数char分隔符拆分字符串 - string.split可能是更好的解决方案

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. 当然,在这种情况下, string.Split是一个更简单的选项,正如其他人所指出的那样。

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 . 见演示

在此输入图像描述

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

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