后台生成文本框字符串,替换文本框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();
}
运行结果
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();
}