根据分割符把字符串生成数组,我现在的代码是这样的
string[] sArray=Regex.Split(valuetemp,"||",RegexOptions.IgnoreCase);但是 11"2||2"||33||44
这样字符串,我想生成
11
2||2
33
44
的形式
也就是说双引号内的||不做为分割符号处理
string[] sArray=Regex.Split(valuetemp,"||",RegexOptions.IgnoreCase);但是 11"2||2"||33||44
这样字符串,我想生成
11
2||2
33
44
的形式
也就是说双引号内的||不做为分割符号处理
string[] sarray = str.Split('\"');
ArrayList al = new ArrayList();
for(int i=0;i<sarray.Length;i++)
{
if (sarray[i].Length == 0)
{
continue;
}
if(i%2==0)
{
string[] nsa = sarray[i].Replace("||", "|").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < nsa.Length; j++)
{
al.Add(nsa[j]);
}
}
else
{
al.Add(sarray[i]);
}
}
al为结果,其它方法不会
string str = "11\"2||2\"||33||44";
string sPtn = @"""[^""]*""(((?'open'"")[^""]*)+((?'-open'"")[^""]*)+)*(?(open)(?!))";
MatchCollection m = Regex.Matches(str, sPtn);
string[] sSet = new string[m.Count];
for(int i=0;i<m.Count;i++)
{
sSet[i] = m[i].Value;
str = str.Replace(m[i].Value, string.Format("{{0}}", i));
}
string[] splitSet = Regex.Split(str,@"\|\|");//splitSet 你要的数组
for (int i = 0; i < splitSet.Length; i++)
{
if(Regex.Match(splitSet[i],@"\{\d+\}").Success)
splitSet[i] = string.Format(splitSet[i], sSet[i]); Response.Write(splitSet[i] + "<br>");
}
输出
11"2||2"
33
44
private string[] RegSplit(string str)
{
string sPtn = @"""[^""]*""(((?'open'"")[^""]*)+((?'-open'"")[^""]*)+)*(?(open)(?!))";
MatchCollection m = Regex.Matches(str, sPtn);
string[] sSet = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
sSet[i] = m[i].Value;
str = str.Replace(m[i].Value, "{"+i+"}");
}
string[] splitSet = Regex.Split(str, @"\|\|");
for (int i = 0, j = 0; i < splitSet.Length; i++)
{
MatchCollection ms = Regex.Matches(splitSet[i], @"\{(\d+)\}");
foreach (Match match in ms)
{
splitSet[i] = Regex.Replace(splitSet[i],"[{]" + j + "[}]", sSet[j]);
j++;
}
}
return splitSet;
}
调用:
string str = "11\"3||3\"aaa\"2||2\"||33||44";
string[] arr = RegSplit(str);
oickf 的方法没有运行起来,在调试CutBug 的第一个方法
结果是错误的
返回结果是
11{0}
33
44第二个方式,可以得出正确结果
不过提示我match 没有使用
不知道如何改善?
StringSplitOptions是2005下的类,
我用的2003
___________________
这个不影响的,下面换成for就不会有提示了,(:
private string[] RegSplit(string str)
{
string sPtn = @"""[^""]*""(((?'open'"")[^""]*)+((?'-open'"")[^""]*)+)*(?(open)(?!))";
MatchCollection m = Regex.Matches(str, sPtn);
string[] sSet = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
sSet[i] = m[i].Value;
str = str.Replace(m[i].Value, "{"+i+"}");
}
string[] splitSet = Regex.Split(str, @"\|\|");
for (int i = 0, j = 0; i < splitSet.Length; i++)
{
MatchCollection ms = Regex.Matches(splitSet[i], @"\{(\d+)\}");
for(int k = 0;k<ms.Count;k++)
{
splitSet[i] = Regex.Replace(splitSet[i],"[{]" + j + "[}]", sSet[j]);
j++;
}
}
return splitSet;
}
{
string str="11\"2||2\"||33||44";
string[] arr = str.Split('"');
ArrayList al = new ArrayList();
for (int i = 0; i < arr.Length; i++)
{
if (i%2!=0) al.Add(arr[i]);
else
{
string[] tmp = arr[i].Replace("||","|").Split('|');
for (int j = 0; j < tmp.Length ; j++)
{
if(tmp[j].Trim().Length!=0 ) al.Add(tmp[j]);
}
}
}
//结果:
for (int i = 0; i < al.Count ; i++)
{
Console.WriteLine(al[i]);
}
}
这样就是你要的结果了 string[] arr = RegSplit("11\"2||2\"||33||44");
/*
结果:
11
"2||2"
33
44
*/ static string[] RegSplit(string str)
{
string sPtn = @"""[^""]*""(((?'open'"")[^""]*)+((?'-open'"")[^""]*)+)*(?(open)(?!))";
MatchCollection m = Regex.Matches(str, sPtn);
string[] sSet = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
sSet[i] = m[i].Value;
str = str.Replace(m[i].Value, "{" + i + "}");
}
string[] splitSet = Regex.Split(str, @"\|\||(?=\{)");
for (int i = 0, j = 0; i < splitSet.Length; i++)
{
MatchCollection ms = Regex.Matches(splitSet[i], @"\{(\d+)\}");
foreach (Match match in ms)
{
splitSet[i] = Regex.Replace(splitSet[i], "[{]" + j + "[}]", sSet[j]);
j++;
}
}
return splitSet;
}
11"2\"||\"2"||33||44
我读出后,通过您的方法
我期待的结果是
11
2"||"2
33
44其实文本是这样的数据
[sectionname]
keyname1 = "valuename1" || $注释1 =前是KEY,=后有""是DATA,没有""的是变量.
keyname2 = "valuename2" || keyname1 $注释2 $后是注释上面的记录我要读KEY 和 DATA 和注释,这些东西是通过||,$这些符号分割的
但是在""里面的符号,不算是这里使用的符号
这里的问题,都是出现在DATA里面,比如说""里面出现符号,出现"(客户的说明是DATA里的"用\"代替了)
我读keyname2的值的时候,应该是valuename2valuename1
string[] arr = RegSplit("11\"2||2\"||33||44");
/*
这不是你要的结果?
11
"2||2"
33
44
*/
11"2\"||\"2"||33||44 到这里应该是
string[] arr = RegSplit("11"\"2||2\""||33||44");11
2"||"2
33
44
11"2\"||\"2"||33||44 到这里应该是
string[] arr = RegSplit("11"2\"||\"2"||33||44");
11
2"||"2
33
44
能这样赋值string吗?没转义肯定报错的,你先确定这个string怎么来写...
"2\"||\"2"||33||44 那转义完,在程序里应该是
string[] arr = RegSplit("\"2\\\"||\\\"2\"||33||44");
"2\"||\"2" (也可以是 "2"||"2" 这样的最好),其实就是把\"不当作引号处理
33
44
{
string[] arr = RegSplit("\"2\\\"||\\\"2\"||33||44");
/*"2\"||\"2"
33
44
*/
} static string[] RegSplit(string str)
{
string sPtn = @"((?<!\\)"")[^\1]*\1(((?'open'"")[^""]*)+((?'-open'"")[^""]*)+)*(?(open)(?!))";
MatchCollection m = Regex.Matches(str, sPtn);
string[] sSet = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
sSet[i] = m[i].Value;
str = str.Replace(m[i].Value, "{" + i + "}");
}
string[] splitSet = Regex.Split(str, @"\|\||(?=\{)");
for (int i = 0, j = 0; i < splitSet.Length; i++)
{
MatchCollection ms = Regex.Matches(splitSet[i], @"\{(\d+)\}");
foreach (Match match in ms)
{
splitSet[i] = Regex.Replace(splitSet[i], "[{]" + j + "[}]", sSet[j]);
j++;
}
//Console.WriteLine(splitSet[i]);
}
return splitSet;
}
string[] arr = RegSplit("\"2\\\"||\\\"2\"||33||\"44\"");
结果应该是
/*
"2"||"2"
33
"44"
*/ \"不算双引号,双引号里面的||不作为分割符号
然后把字符串用||分开
{
string[] arr = RegSplit("\"1\\\"2||3\\\"4\"||5||6\"7\\\"||\\\"8\" ");
} static string[] RegSplit(string str)
{
string sPtn = @"((?<!\\)"")((?:(?<=\\)"")|[^""])*\1(((?'open'\1)\2*)+((?'-open'\1)\2*)+)*(?(open)(?!))"; MatchCollection m = Regex.Matches(str, sPtn);
string[] sSet = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
sSet[i] = m[i].Value;
str = str.Replace(m[i].Value, "{" + i + "}");
}
string[] splitSet = Regex.Split(str, @"\|\||(?>=\{)");
for (int i = 0, j = 0; i < splitSet.Length; i++)
{
MatchCollection ms = Regex.Matches(splitSet[i], @"\{(\d+)\}");
foreach (Match match in ms)
{
splitSet[i] = Regex.Replace(splitSet[i], "[{]" + j + "[}]", sSet[j]);
splitSet[i] = splitSet[i].Replace("\\\"", "\"");
j++;
}
Console.WriteLine(splitSet[i]);
}
return splitSet;
}
输出:
"1"2||3"4"
5
6"7"||"8"
请按任意键继续. . .