求一个正则的写法 需求是从htm代码里提取表单中 input type="radio" 选中的 value

aspvfp 2017-10-25 07:00:24

<p>用户名称: <input type="text" name="Name" style="width: 300px" /></p>
<p>性别:
<input type="radio" name="sex" value="男"><br><input type="radio" name="sex" value="女" checked>

<p>客户电话: <input type="text" name="CustomerTel" style="width: 300px" /></p>
<p>客户类型: <input type="radio" name="ctype" value="VIP" >VIP <br><input type="radio" name="ctype" value="钻石" checked>钻石 <input type="radio" name="ctype" value="黄金" >黄金</p>
.....
<p>客户地址: <input type="text" name="CustomerAddress" style="width: 500px" /></p>
........


我需要取
<input type="radio" name="sex" value="女" checked>
<input type="radio" name="ctype" value="钻石" checked>

我自己大概写了个

    public static string GetRegexInputradioValue(string html, string name)
{

try
{
string pattern = @"<input type=""radio""[\s\S]*?name=""(?<name>.*?)""[\s\S]*?value=""(?<value>.*?)""[\s\S]*?checked>";

if (Regex.IsMatch(html, pattern))
{
Match math = Regex.Match(html, pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
while (math.Success)
{

if (name == math.Groups["name"].Value)
{
return math.Groups["value"].Value;
}
math = math.NextMatch();
}

}
return string.Empty;

}
catch (Exception ex)
{

return string.Empty;
}
}
//调用
GetRegexInputradioValue(html,"sex")
GetRegexInputradioValue(html,"ctype")

但是取第一个可以取出来 取到第二个 慢的不得了 后来用调试时候发现 取到这样的东西

<input type="radio" name="sex" value="女" checked>女
...........
<p>客户电话: <input type="text" name="CustomerTel" style="width: 300px" /></p>
<p>客户类型: <input type="radio" name="ctype" value="VIP" >VIP <br><input type="radio" name="ctype" value="钻石" checked>


...全文
125 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mirrorspace 2017-10-26
  • 打赏
  • 举报
回复
引用NSoup.dll http://nsoup.codeplex.com/ 用这个,可用选器找DOM,就像在前端使用脚本一样
闭包客 2017-10-26
  • 打赏
  • 举报
回复
重提一下,只使用正则表达式,无法实现完全的 html 解析。
闭包客 2017-10-26
  • 打赏
  • 举报
回复
你需要一个 html 解析器: http://blog.csdn.net/closurer/article/details/73466685

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧