对于一个项目,我需要将字符串值分隔成一个字符串列表。 是建立如下的字符串:正则表达式排除字符串到数组的模式
string unprocessed = "\"foo,bar\",\"foobar\",\"shizzle ma nizzle\"";
我想INT进入类似如下的数组:
string[] processed = [] { "\"foo,bar\"", "\"foobar\"", "\"shizzle ma nizzle\""};
为此,使用正则表达式匹配系统,该代码分离的IM “,”字符组合。我到目前为止的代码如下:
Regex reg = new Regex(@"((?!(,""|"",)).)+");
string regmatch = "\"\"wubba,lubba\",\"dup dub\"\"";
var matches = reg.Matches(regmatch);
Assert.AreEqual(2, matches.Count);
Assert.AreEqual("\"dup dub\"\"", matches[1].Value); // passes
Assert.AreEqual("\"\"wubba,lubba\"", matches[0].Value); // fails because value = \"\"wubba,lubba
到目前为止,我得到一个微小的错误,如示例代码所示。现在我在做东西,我快到了。有人可以帮我解决这个正则表达式问题吗?还是有更好的方法来做到这一点?
你为什么不干脆用String .Split(String [],StringSplitOptions)? –
@DavideVisentin和什么应该作为String []传递? –
@SergeyBerezovskiy新字符串[] {“\,\”}。 –