C#截取字符串
请教高手,我有个字符串比如Z04[7,10]=Z02[3,6]+XUED[5,6]-IRH[2,6],现在希望能截出Z04[7,10],Z02[3,6],IRH[2,6]使用,请问应该怎么做? --------------------编程问答-------------------- 如果每组数据长度都一样,可以用substring如果不一样正则好点吧 --------------------编程问答--------------------
--------------------编程问答--------------------
Regex re = new Regex(@"\b[A-Za-z0-9]{3}\[\d+,\d+\]", RegexOptions.None);
MatchCollection mc = re.Matches("你要提取字符串");
foreach (Match ma in mc)
{
//ma.Value就是你要的值
}
--------------------编程问答-------------------- --------------------编程问答-------------------- string.split --------------------编程问答-------------------- 就是取等号左边那个、等号右边第一个和最后一个是吧:
Regex re = new Regex(@"\b[A-Za-z0-9]{3}\[\d+,\d+\]", RegexOptions.None);
MatchCollection mc = re.Matches("text");
foreach (Match ma in mc)
{
//ma.Value就是你要的值
}
string s = "Z04[7,10]=Z02[3,6]+XUED[5,6]-IRH[2,6]";
MatchCollection matches = Regex.Matches(s, @"(?<=[=+-]?)\w+\[\d+,\d+\](?=[=+-]?)");
for (int i = 0; i < matches.Count; i++)
if (i < 2 || i == matches.Count - 1)
Response.Write(matches[i].Value + "<br/>");
输出:
Z04[7,10]
Z02[3,6]
IRH[2,6]
--------------------编程问答-------------------- Z04[7,10]=Z02[3,6]+XUED[5,6]-IRH[2,6]
为什么不要XUED[5,6],规则是什么? --------------------编程问答-------------------- string[] 你要的数组=原始数组.split('+','-','*','/') --------------------编程问答--------------------
+1
--------------------编程问答--------------------
--------------------编程问答--------------------
string s = "Z04[7,10]=Z02[3,6]+XUED[5,6]-IRH[2,6]";
string[] ss = s.Split("+-*/=".ToCharArray());
--------------------编程问答-------------------- String.Split
string s = "Z04[7,10]=Z02[3,6]+XUED[5,6]-IRH[2,6]";
string[] ss = s.Split("+-*/=".ToCharArray());
补充:.NET技术 , C#