C# 逗号、空格或分号分隔字符串上的 Regex.Split()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14689044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex.Split() on comma, space or semi-colon delimitted string
提问by bflemi3
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"]
采纳答案by Cédric Bignon
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
.
在这种情况下,您希望按特定的分隔符字符拆分字符串。String.Split
已为此特殊目的而创建。这种方法会比Regex.Split
.
char[] delimiters = new [] { ',', ';', ' ' }; // List of your delimiters
var splittedArray = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
回答by DarkWalker
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
还要检查下面的答案,如果您真正需要的是根据几个字符分隔符拆分字符串 - string.split 可能是更好的解决方案
回答by JDB still remembers Monica
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:
您正在@
为字符串使用符号,因此将"\"
其解释为文字斜杠。因此,您的字符类实际上是读取为 a "\"
、 an "s"
、 a","
或 a ";"
。删除多余的斜杠,它应该可以按需要工作:
var values = Regex.Split("22222, 11111, 23232", @"[\s,;]+")
回答by Ani
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 notbe 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
,正如其他人所指出的那样,这是一个更简单的选择。