读书人

急用正则表达式 婚配一串字符并取出字

发布时间: 2012-12-16 12:02:32 作者: rapoo

急!用正则表达式 匹配一串字符并取出字段
功能实现:一串字符:“result={A}&description={B}&faillist={C}”
除了{A}{B}{C}是变量 其他都是固定的。我现在 想要的是 匹配这串字符 并且将{A}{B}{C}取出来。能实现么?正则该怎么写。我是菜鸟 求指导!!!!
[最优解释]
result参数能保证一定有吗?如果能,用下面的,注意取结果时用了命名捕获组

string test = "result=1&description=返回出错&faillist=13656645185,14587485478";
Regex reg = new Regex(@"(?i)result=(?<result>[^&]*)(?:&description=(?<description>[^&]*))?(?:&faillist=(?<faillist>[^&=]*))?");
Match m = reg.Match(test);
if (m.Success)
{
richTextBox2.Text += "result:" + m.Groups["result"].Value + "\n";
richTextBox2.Text += "description:" + m.Groups["description"].Value + "\n";
richTextBox2.Text += "faillist:" + m.Groups["faillist"].Value + "\n";
}

[其他解释]
想带括号就把 Regex reg_imgformorA = new Regex(
"[\\w\\W]*?result={([\\w\\W]*?)}");
改成 Regex reg_imgformorA = new Regex(
"[\\w\\W]*?result=([\\w\\W]*?)&");
[其他解释]
(?i)result=\{([^{}]*?)\}&description=\{([^{}]*?)\}&faillist=\{([^{}]*?)\}


string str = "result={A}&description={B}&faillist={C}";
MatchCollection ary = Regex.Matches(str, @"(?i)result=\{([^{}]*?)\}&description=\{([^{}]*?)\}&faillist=\{([^{}]*?)\}");
foreach (Match m in ary)
{
Console.WriteLine(m.Value);
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(m.Groups[2].Value);
Console.WriteLine(m.Groups[3].Value);
}

[其他解释]

string str = "result=1&description=返回出错&faillist=13656645185,14587485478";
var ary = Regex.Matches(str, @"(\w*?)=([^$&\s]+)").Cast<Match>().Select(t => new { a = t.Groups[1].Value, value = t.Groups[2].Value }).ToArray();
foreach (var t in ary)
{
Console.WriteLine(t.a + ":" + t.value);
}
[其他解释]
result=([^$&\s]+)[$&\s]description=([^$&\s]+)[$&\s]faillist=([^$&\s]+)
[其他解释]
string stra = "result={A}&description={B}&faillist={C}";


Regex reg_imgformorA = new Regex(
"[\\w\\W]*?result={([\\w\\W]*?)}");
MatchCollection mcpiformorlist = reg_imgformorA.Matches(stra);
if (mcpiformorlist.Count > 0)
{
string resulta = mcpiformorlist[0].Groups[1].Value;
}
Regex reg_imgformorB = new Regex(
"[\\w\\W]*?&description={([\\w\\W]*?)}");
MatchCollection mcpiformorB = reg_imgformorB.Matches(stra);
if (mcpiformorB.Count > 0)
{
string resultb = mcpiformorB[0].Groups[1].Value;
}
Regex reg_imgformorC = new Regex(
"[\\w\\W]*?&faillist={([\\w\\W]*?)}");
MatchCollection mcpiformorC = reg_imgformorC.Matches(stra);
if (mcpiformorC.Count > 0)
{
string resultb = mcpiformorC[0].Groups[1].Value;
}


最后这个resulta 。 resultb.resultc输出的是A B C
[其他解释]
string str = "result={A}&description={B}&faillist={C}";
var ary = Regex.Matches(str, @"(\w*?)=([^$&\s]+)").Cast<Match>().Select(t => new { a = t.Groups[1].Value, value = t.Groups[2].Value }).ToArray();
foreach (var t in ary)
{
MessageBox.Show(t.a + ":" + t.value);
}
[其他解释]



前面两个能匹配出来 ,但是最后一个 不能出来值!是什么原因?


[其他解释]

引用:
引用:string stra = "result={A}&description={B}&faillist={C}";
Regex reg_imgformorA = new Regex(
"[\\w\\W]*?result={([\\w\\W]*?)}");
……

这么写没问题啊,输出A B C
[其他解释]
Regex reg_imgformorD = new Regex("{([\\w\\W]*?)}&description={([\\w\\W]*?)}&faillist={([\\w\\W]*?)}");
MatchCollection mcpiformorlist = reg_imgformorD.Matches(stra);
if (mcpiformorlist.Count > 0)
{
string resulta = mcpiformorlist[0].Groups[1].Value;
string resultb = mcpiformorlist[0].Groups[2].Value;
string resultc = mcpiformorlist[0].Groups[3].Value;
}
[其他解释]
你执行上边这个看看
[其他解释]
引用:
(?i)result=\{([^{}]*?)\}&description=\{([^{}]*?)\}&faillist=\{([^{}]*?)\}



C# code??



12345678910

string str = "result={A}&description={B}&faillist={C}"; Ma……




你好,“{}”也是要输出的。是整个替换掉。。比如
"result=1&description=返回出错&faillist=13656645185,14587485478"
最后一个“13656645185,14587485478”也没有出来
[其他解释]
string str = "result=1&description=返回出错&faillist=13656645185,14587485478";
str =string.Join("\r\n", Regex.Replace(str, @"\w+=", "").Split('&'));
Console.WriteLine(str);

[其他解释]
换成这个试试(?is)result=(\{?[^{}]*?\}?)&description=(\{?[^{}]*?\}?)&faillist=(\{?[^{}]+?\}?)(?=$)
[其他解释]
字符串中间可不可以有空格,
如果有,哪些地方可以有
[其他解释]
不就是匹配URL的参数部分吗?

//string test = "result={A}&description={B}&faillist={C}";
string test = "result=1&description=返回出错&faillist=13656645185,14587485478";
Regex reg = new Regex(@"result=([^&]*)&description=([^&]*)&faillist=([^&=]*)");
Match m = reg.Match(test);


if (m.Success)
{
richTextBox2.Text += "result:" + m.Groups[1].Value + "\n";
richTextBox2.Text += "description:" + m.Groups[2].Value + "\n";
richTextBox2.Text += "faillist:" + m.Groups[3].Value + "\n";
}
/*-----输出-----
result:1
description:返回出错
faillist:13656645185,14587485478
*/


[其他解释]
引用:
Regex reg_imgformorD = new Regex("{([\\w\\W]*?)}&description={([\\w\\W]*?)}&faillist={([\\w\\W]*?)}");
MatchCollection mcpiformorlist = reg_imgformorD.Matches(stra);
……

我知道 问题在哪里了。“{A}{B}{C}”整个是变量。比如“result=1&description=错误描述&faillist=失败号码列表”
然后 输出的是
string resulta = mcpiformorlist[0].Groups[1].Value;//1
string resultb = mcpiformorlist[0].Groups[2].Value;//错误描述
string resultc = mcpiformorlist[0].Groups[3].Value;//失败号码列表

现在按你的 有{}是能出来的 但是 {}去掉之后 就不能显示了。能麻烦再解决下么 谢谢了!

[其他解释]
引用:
不就是匹配URL的参数部分吗?



C# code??



123456789101112131415

//string test = "result={A}&description={B}&faillist={C}"; string test = "result=1&description=返回出错&faillist=1365664518……


就是要这个。谢谢了。但是现在有个问题是“result=1&description=错误描述”这种的 或者“result=1”这种的 要这三种都出现都能匹配 有可能吗?我感觉 应该用个 “
[其他解释]
哦~
[其他解释]
”符号 就好了 可是我试了下 有问题=,=能帮忙 解答下么?
[其他解释]
在这里谢谢各位的回答,辛苦了。分数不多,还请多多包涵!!!
[其他解释]
引用:
result参数能保证一定有吗?如果能,用下面的,注意取结果时用了命名捕获组



C# code??



123456789

string test = "result=1&description=返回出错&faillist=13656645185,14587485478"; Regex reg = new Regex(@"(?i)result=(?<re……


谢谢了 ,成了。虽然 不知道 这一串字符什么意思.....诶。。这个正则 看来得学啊。……

读书人网 >C#

热点推荐