2011-06-29 76 views

回答

1

只需使用Split方法;不需要正则表达式。

string[] parts = mystr.Split('|'); 
+0

对不起卢克,以为我在编辑我的答案,不知道发生了什么事。随时回滚。 –

+0

@Yuriy:不用担心:) – LukeH

+0

我通过帖子回复了我的回复,不知道为什么它没有通过。很奇怪。 –

2

你可以使用String.Split();

string mystr = "[email protected]|Action Required to Activate Membership for ClanTemplates|href="|">|6|6"; 
string[] parts = mystr.Split(new char[] { '|' }); 
+0

我得到错误:1的最佳重载方法匹配“string.Split(PARAMS的char [])”具有一些无效参数 – user812273

+0

看我的样,我是直接使用'mystr.Split()'。 –

+0

示例工程,谢谢 – user812273

0

如果你真的想使用正则表达式,你需要记住逃脱原正则表达式的|\|,并在C#,"\\|"@"\|"

string[] parts = Regex.Split (input, @"\|"); 

对于这样简单的事情,只需使用string[] parts = input.Split('|')。在这种情况下,你不应该使用正则表达式,除非有特别的东西,比如不想在逃脱的管道上分割(比如[email protected]|my value has a \| in it|more stuff')。在这个例子中,你可以使用这个:

string[] parts = Regex.Split (input, @"(?<!\\)\|");