首页 新闻 会员 周边

c#中正则匹配成功后Groupes[0].Value的字符串丢失结尾部分字符

0
悬赏园豆:5 [待解决问题]

后台生成文本框字符串,替换文本框4,5的值和宽度

static void Main(string[] args)
{

string str = "<input name='txtgx1' type='text' id='txtgx1' value='' style='width:30px;'/><input name='txtgx2' type='text' id='txtgx2' value='' style='width:30px;'/><input name='txtgx3' type='text' id='txtgx3' value='' style='width:30px;'/><br/><input name='txtgx4' type='text' id='txtgx4' value='' style='width:30px;'/>;<br/><input name='txtgx5' type='text' id='txtgx5' value='' style='width:30px;'/>";
string r = "";
string strp1 = "(.+)(<input name='txtgx4'.+ )";
string strp2 = "value=.+?/>";
string strp3 = ("value='完好' style='width:100px;'/>");
Match m = Regex.Match(str, strp1);
if (m.Success)
{
string reg = Regex.Replace(m.Groups[2].Value, strp2, strp3);
r = m.Groups[1].Value + reg;
}
Console.WriteLine("原字符串:"+str);
Console.WriteLine("字符串[0]:" + m.Groups[0].Value);
Console.WriteLine("字符串[2]:" + m.Groups[2].Value);
Console.WriteLine("替换后的字符串:" + r);
Console.ReadLine();

}

运行结果

问题补充:

不明白的地方strp1正则是贪婪模式怎么会丢失字符,正则表达式写错了吗?

易顺金的主页 易顺金 | 初学一级 | 园豆:2
提问于:2014-12-08 10:13
< >
分享
所有回答(1)
0

static void Main(string[] args)
{
string str = "<input name='txtgx1' type='text' id='txtgx1' value='' style='width:30px;'/><input name='txtgx2' type='text' id='txtgx2' value='' style='width:30px;'/><input name='txtgx3' type='text' id='txtgx3' value='' style='width:30px;'/><br/><input name='txtgx4' type='text' id='txtgx4' value='' style='width:30px;'/>;<br/><input name='txtgx5' type='text' id='txtgx5' value='' style='width:30px;'/>";
string r = "";
string strp1 = "(.+)(<input name='txtgx4'.+ )(.+)";
string strp2 = "value=.+/>";
string strp3 = ("value='完好' style='width:100px;'/>");
Match m = Regex.Match(str, strp1);
string reg = "";
if (m.Success)
{
reg = Regex.Replace(m.Groups[2].Value, strp2, strp3);
r = m.Groups[1].Value + reg + m.Groups[3].Value;
}
Console.WriteLine("原字符串:" + str);
Console.WriteLine("字符串[0]:" + m.Groups[0].Value);
Console.WriteLine("字符串[2]:" + m.Groups[2].Value);
Console.WriteLine("reg:" + reg);
Console.WriteLine("后字符串:" + r);
Console.ReadLine();
}

Slark.NET | 园豆:692 (小虾三级) | 2014-12-08 11:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册