對於一個項目,我需要將字符串值分隔成一個字符串列表。 是建立如下的字符串:正則表達式排除字符串到數組的模式
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新字符串[] {「\,\」}。 –