求一正则表单式
想找出网页中含有 hidden 标签中的 name 和value
<input type="hidden" name="hf10" value="89038" />
<input type="hidden" name="hf20" value="B" />
<input type="hidden" name="phf222" value="对" /
<input type="hidden" name="hf10" value="89038" />
<input type="hidden" name="hf20" value="B" />
<input type="hidden" name="phf222" value="对" /
private List<string> GetElementContent(string content, string elementName, string subElement)
{
StringBuilder regexStr = new StringBuilder("(?is)<");
regexStr.Append(elementName).Append("[^>]*?").Append(subElement).Append(@"=(['""\s]?)([^'""\s]+)\1[^>]*?>");
Regex regex = new Regex(regexStr.ToString());
MatchCollection match = regex.Matches(content);
List<string> values = new List<string>();
foreach (Match m in match)
{
values.Add(m.Groups[2].Value);
}
return values;
}
//调用
List<string> values = GetElementContent(retString, "input '", "value"); //如果这样 正则为 {(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>}
List<string> names = GetElementContent(retString, "input type='hidden'", "name"); //如果这样 正则为 /{(?is)<input type='hidden'[^>]*?name=(['"\s]?)([^'"\s]+)\1[^>]*?>}
...全文
请发表友善的回复…
发表回复
EnForGrass 2016-10-14
- 打赏
- 举报
为什么要分开呢?直接一步到位
private static Dictionary<string,string> GetElementContent(string content, string elementName)
{
StringBuilder regexStr = new StringBuilder("(?is)<");
regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
Regex regex = new Regex(regexStr.ToString());
MatchCollection match = regex.Matches(content);
Dictionary<string, string> values = new Dictionary<string, string>();
foreach (Match m in match)
{
values.Add(m.Groups[1].Value, m.Groups[2].Value);
}
return values;
}
Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*type\s*=\s*['""]hidden['""]");
names.ToList().ForEach(x => Console.WriteLine("name:" + x.Key + ",value:" + x.Value));
[/quote]
这个是的啊 ,不过type name value之间也有可能有其他tag吧 光只有这3个属性倒是很好匹配
主要不知道是不是只有楼主那个例子那样的情况[/quote]
改一下就行了啊
Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*[^>]*?type\s*=\s*['""]hidden['""]");
regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+[^>]*?value\s*=\s*['""]([\w\W]+?)['""][^>]*?/>");
stherix 2016-10-14
- 打赏
- 举报
为什么要分开呢?直接一步到位
private static Dictionary<string,string> GetElementContent(string content, string elementName)
{
StringBuilder regexStr = new StringBuilder("(?is)<");
regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
Regex regex = new Regex(regexStr.ToString());
MatchCollection match = regex.Matches(content);
Dictionary<string, string> values = new Dictionary<string, string>();
foreach (Match m in match)
{
values.Add(m.Groups[1].Value, m.Groups[2].Value);
}
return values;
}
Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*type\s*=\s*['""]hidden['""]");
names.ToList().ForEach(x => Console.WriteLine("name:" + x.Key + ",value:" + x.Value));
[/quote]
这个是的啊 ,不过type name value之间也有可能有其他tag吧 光只有这3个属性倒是很好匹配
主要不知道是不是只有楼主那个例子那样的情况EnForGrass 2016-10-14
- 打赏
- 举报
为什么要分开呢?直接一步到位
private static Dictionary<string,string> GetElementContent(string content, string elementName)
{
StringBuilder regexStr = new StringBuilder("(?is)<");
regexStr.Append(elementName).Append("[^>]*?\\s+").Append(@"name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
Regex regex = new Regex(regexStr.ToString());
MatchCollection match = regex.Matches(content);
Dictionary<string, string> values = new Dictionary<string, string>();
foreach (Match m in match)
{
values.Add(m.Groups[1].Value, m.Groups[2].Value);
}
return values;
}
Dictionary<string, string> names = GetElementContent(htmlcontent, @"input\s*type\s*=\s*['""]hidden['""]");
names.ToList().ForEach(x => Console.WriteLine("name:" + x.Key + ",value:" + x.Value));
stherix 2016-10-14
- 打赏
- 举报
单你这个例子的话
<input type="hidden".*name="([^"]+)" 可以找出name
<input type="hidden".*value="([^"]+)" 可以找出value
EnForGrass 2016-10-14
- 打赏
- 举报
或者
var htmlcontent = @"<input type=""hidden"" name=""hf10"" value=""89038"" />fdasfdsa<input type = ""hidden"" name = ""hf20"" value = ""B"" /><input type = ""hidden"" name = ""phf222"" value = ""对"" /> ";
foreach (Match reg in Regex.Matches(htmlcontent, @"(?is)<input\s+type\s*=\s*['""]hidden['""]\s+name\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>"))
{
// Console.WriteLine(reg.Value);
Console.WriteLine("name:"+reg.Groups[1].Value + ",value:"+ reg.Groups[2].Value);
}
EnForGrass 2016-10-14
- 打赏
- 举报
private static List<string> GetElementContent(string content, string elementName, string subElement)
{
StringBuilder regexStr = new StringBuilder("(?is)<");
regexStr.Append(elementName).Append("[^>]*?\\s+").Append(subElement).Append(@"\s*=\s*['""]([\w\W]+?)['""]\s+value\s*=\s*['""]([\w\W]+?)['""]\s*/>");
Regex regex = new Regex(regexStr.ToString());
MatchCollection match = regex.Matches(content);
List<string> values = new List<string>();
foreach (Match m in match)
{
values.Add(m.Groups[1].Value);
}
return values;
}
List<string> names = GetElementContent(retString, @"input\s*type\s*=\s*['""]hidden['""]", "name");
names.ForEach(x => Console.WriteLine(x));
风淡云清2013 2016-10-14
- 打赏
- 举报
{(?is)<input[^>]*?value=(['"\s]?)([^'"\s]+)\1[^>]*?>} 关键这句怎么改
stherix 2016-10-14
- 打赏
- 举报
以我的正则能力是写不出来
可以考虑其他方法啊
比如jquery(外部页面是没办法了)
比如利用DOM (webbroswer的话)
比如xml遍历查找
风淡云清2013 2016-10-14
- 打赏
- 举报
List<string> values = GetElementContent(retString, "input", "value");
List<string> names = GetElementContent(retString, "input type='hidden'", "name"); 博客介绍了HTML中表格、表单和框架的相关知识。表格由、
| 标签定义,涉及colspan和rowspan属性。表单用于收集客户端信息,包含多种控件。框架可将浏览器窗口划分为多个区域,通过 |